TextBox输入未分配给变量

时间:2014-05-07 06:09:11

标签: c# wpf textbox

我正在编写一个小程序,它接受用户值,将其转换为int,将其乘以60,然后将该int值附加到cmd命令。 我的所有代码都运行正常,包括xaml部分,但是我没有在cmd命令的末尾获取int值,它在末尾附加了一个零(0)。

这是我程序的C#代码。

// New string to store TextBox value
private string _timer;
public string timer
{
    get
    {
        return _timer;
    }
    set
    {
        _timer = timer;
    }

}

// TextBox input into a variable
private void timerEnter_Text(object sender, TextChangedEventArgs e)
{
    timer = textBoxTimer.Text;      // Here lies the problem, textbox.Text has the value but it isn't getting assigned to timer
}

// Convert timer into int for conversion
private int _timerNum;
public int timerNum
{
    get
    {
        return _timerNum;
    }
    set
    {
        _timerNum = Convert.ToInt32(timer);
    }

}

// Convert timerNum into timerSec
private int _timerSec;
public int timerSec
{
    get
    {
        return _timerSec;
    }
    set
    {
        _timerSec = timerNum * 60;
    }
}

// Setting the cmdName
public string cmdName { get; set; }

// ComboBox item commands
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ComboBox.SelectedItem == null)
        return;

    if (ComboBox.SelectedItem == fastshutdown)
    {
        cmdName = "/C shutdown /s /hybrid /t ";
    }
}

// Declare a new variable cmd which appends timerSec with cmdName
public string cmd { get; set; }

private void setTimer_Click(object sender, RoutedEventArgs e)
{
    timer = textBoxTimer.Text;
    cmd = cmdName + timerSec;           // Here timerSec remains null (0) and hence any action is immediate
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = cmd;
    process.StartInfo = startInfo;
    process.Start();
}

这是文本框的XAML代码

<TextBox Name="textBoxTimer"
 Width="100"
 HorizontalAlignment="Center"
 HorizontalContentAlignment="Center"
 Margin="0,0,0,0"
 TextChanged="timerEnter_Text">
 </TextBox>

我的问题是文本框可以接受该值,并且在调试时我可以看到textBoxTimer.Text具有值但是timer的值仍然为null,因此变量timerSec为0。

这可能有什么问题?

更新:现在正在分配计时器,但是timerSec仍为0 所以我添加了这些行,但它说不能隐式地将类型字符串转换为int,但我已经有了函数

private void setTimer_Click(object sender, RoutedEventArgs e)
    {
        timer = textBoxTimer.Text;
        timerNum = timer;              // Newly added, here the error says cannot convert implicitly
        timerSec = timerNum;           // Newly added
        cmd = cmdName + timerSec;           // Here timerSec remains null (0) and hence any action is immediate
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = cmd;
        process.StartInfo = startInfo;
        process.Start();
    }

2 个答案:

答案 0 :(得分:4)

这就是问题

get
{
    return _timer;
}
set
{
    _timer = value; // change timer to value
}

答案 1 :(得分:0)

要回答您更新的问题:

timerNum被声明为int,而timerstring。因此,您需要将string转换为int,如下所示:

timerNum = Int32.Parse(timer);