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

假装没事ソ 提交于 2019-12-02 00:37:31

问题


I have a NumericUpDown and I need when value change (and not lostfocus) do a new calculation

if i put my code in event ValueChanged this work when focus is lost

if i put my code in KeyPress then if number is not enter by keyboard (example copy a number and paste it) it doesn't work

then what event do i need use?

and if this is keypress i need concatenate the numeric value more the key pressed convert all this to string and convert it to decimal, and do the calculate, but it does not work if key pressed is not a number (example backspace)


回答1:


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


来源:https://stackoverflow.com/questions/17369670/how-to-validate-numericupdown-when-value-change-and-not-lost-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!