Pregunta

I am writing a small program which takes a user value, converts it into int, multiplies it by 60 and then appends that int value to cmd command. All of my code works fine including the xaml part but I donot get the int value at the end of the cmd command, it appends a zero (0) at the end.

Here is the C# code for my program.

// 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();
}

And here is the XAML code for the textbox

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

My problem is that the textbox can accept the value and while debugging I can see that textBoxTimer.Text has the value but the value of timer still remains null and hence the variable timerSec is 0.

What might be wrong with this ??

UPDATE: timer is now being assigned, but timerSec is still 0 So I added these lines but it says cannot implicitly convert type string to int, but I already have the function in place

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();
    }
¿Fue útil?

Solución

this is the issue

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

Otros consejos

To answer your updated question:

timerNum is declared as int, while timer is a string. So you need to convert your string to int, like this:

timerNum = Int32.Parse(timer);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top