值更改时(而不是失去焦点时)如何验证numericupdown [英] how to validate numericupdown when value change (and not lost focus)

查看:235
本文介绍了值更改时(而不是失去焦点时)如何验证numericupdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NumericUpDown,我需要在值更改(而不是失去焦点)时进行新的计算

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

如果我将代码放在事件ValueChanged中,则在失去焦点时会起作用

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

如果我将代码放在KeyPress中,则如果未通过键盘输入数字(例如,复制并粘贴数字),将无法正常工作

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)

推荐答案

您可以使用KeyUp事件检查直接编辑,并使用CTRL + V粘贴操作.

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

然后,您可以收听MouseUp事件以使用鼠标右键(上下文菜单)检查粘贴操作.

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

在此示例代码中,如果用户输入的数字大于9,则会显示MessageBox:

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

这篇关于值更改时(而不是失去焦点时)如何验证numericupdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆