ValueChanged event of the numericUpDown control doesn't fire when a user types in a value (instead of clicking on the up/down buttons).
That's because the value *didn't* change, the *text* changed. The Value cannot be null which is obvious because Value is a decimal, i.e., a ValueType, which can never be null.
The NumericUpDown control has a TextChanged event you can use. However, it does not appear on the Event page of the properties for the control, nor does it appear in IntelliSense combobox when typing.
The NumericUpDown has had its Text property its TextChanged event hidden because they are not intended to be used generally. Note that the ValueChanged event is not raised until the Value property actually changes. When the user types directly into the NUD the Value property does not change until the Text is validated, in case the user types in an invalid value. Note that the event is raised immediately when you click an arrow button because the control knows that the Text will be valid.
numericUpDown1.Validating += new CancelEventHandler(numberupdownValidating0);
numericUpDown1.ValueChanged += new EventHandler(numberupdownChanged0);
numericUpDown1.TextChanged += new EventHandler(numericUpDown_TextChanged);
private void numericUpDown_TextChanged(object sender, EventArgs e)
{
NumericUpDown nud = (NumericUpDown)sender;
try
{
int i = Convert.ToInt32(nud.Text);
nud.Value = i; // fire the event numberupdownChanged0
}
catch (Exception ex)
{
errorProvider.SetError(nud, ex.Message);
nud.Tag = false;
ValidateOKButton();
}
}
private void numberupdownChanged0(object sender, EventArgs e)
{
NumericUpDown nud = (NumericUpDown)sender;
if (nud.Value >= 0)
{
errorProvider.SetError(nud, "");
nud.Tag = true;
}
else
{
errorProvider.SetError(nud, "Value must be equal or greater than 0");
nud.Tag = false;
}
ValidateOKButton();
}
private void numberupdownValidating0(object sender, CancelEventArgs e)
{
NumericUpDown nud = (NumericUpDown)sender;
if (nud.Value >= 0)
{
errorProvider.SetError(nud, "");
nud.Tag = true;
}
else
{
errorProvider.SetError(nud, "Value must be equal or greater than 0");
nud.Tag = false;
}
ValidateOKButton();
}
public void ValidateOKButton()
{
btnOk.Enabled = ((bool)(textBox_connectionStr.Tag)) && ((bool)(numericUpDown1.Tag)) && ((bool)(numericUpDown2.Tag)) && ((bool)(numericUpDown3.Tag)) && ((bool)(numericUpDown4.Tag)) && ((bool)(numericUpDown5.Tag));
}