how to validate numericupdown when value change (and not lost focus)

蹲街弑〆低调 提交于 2019-12-01 21:59:33

You can use KeyUp event to check direct editing and paste operations with CTRL+V.

Then you can listen to MouseUp event to check paste operations with right mouse button (context menu).

In this sample code a MessageBox is shown if user inputs a number greater than 9:

private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
    if (numericUpDown1.Value >= 10){
       numericUpDown1.Value = 0;
       MessageBox.Show("number must be less than 10!");
    }
}

private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right) {
       if (numericUpDown1.Value >= 10){
           numericUpDown1.Value = 0;
           MessageBox.Show("number must be less than 10!");
       }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!