NumericUpDown validate

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



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值