9

I am using Microsoft Visual C# 2010 Express. When i change the value of numericUpDown using arrows, my button becomes enable. But i also want to enable my button when i change the value of numericUpDown by changing the text directly.

I am using the following code:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}
O. R. Mapper
  • 18,591
  • 9
  • 58
  • 102
Lany
  • 133
  • 2
  • 12
  • And what is the problem? – Abbas Jul 09 '13 at 07:42
  • 1
    Try to focus out the cursor of the numericUpDown control after you change the number – Donatas K. Jul 09 '13 at 07:43
  • Or press enter. You could also just subscribe to keypress event, but remember that the text isn't yet validated so if you call the value there you'll get the old value. If however you call the numericUpdown value inside the button.Click event it will be changed already. – Martheen Jul 09 '13 at 07:47
  • problem is that when i change the value directly, button is not being enabled – Lany Jul 09 '13 at 08:13

1 Answers1

20

You may need to use TextChanged event instead of using ValueChanged. The Value changed event need you to press enter key after changing value to get ValueChanged fired.

What MSDN say about NumericUpDown.ValueChanged Event

For the ValueChanged event to occur, the Value property can be changed in code, by clicking the up or down button, or by the user entering a new value that is read by the control. The new value is read when the user hits the ENTER key or navigates away from the control. If the user enters a new value and then clicks the up or down button, the ValueChanged event will occur twice, MSDN.

Binding TextChanged event.

private void TestForm_Load(object sender, EventArgs e)
{
    numericUpDown1.TextChanged += new EventHandler(numericUpDown1_TextChanged);
}

Declaration of TextChanged event.

void numericUpDown1_TextChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}
Adil
  • 139,325
  • 23
  • 196
  • 197
  • Is it possible that as i changes the value, the button becomes enabled , without pressing the enter?? – Lany Jul 09 '13 at 08:16
  • yeah i have tried that...not applicable here. but yeah it can be done by pressing enter button. btw thankz :) – Lany Jul 09 '13 at 12:58