[英]how to validate numericupdown when value change (and not lost focus)
我有一個NumericUpDown
,我需要在值更改(而不是lostfocus)時進行新的計算
如果我把我的代碼放在事件ValueChanged
這項工作時焦點丟失
如果我把我的代碼放在KeyPress
然后如果數字不是通過鍵盤輸入(例如復制一個數字並粘貼它)它不起作用
那么我需要使用什么事件?
如果這是按鍵,我需要連接數值,按下的鍵將所有這些轉換為字符串並將其轉換為十進制,並執行計算,但如果按下的鍵不是數字,則不起作用(示例退格)
您可以使用KeyUp
事件檢查CTRL + V的直接編輯和粘貼操作。
然后,您可以使用鼠標右鍵(上下文菜單)監聽MouseUp
事件以檢查粘貼操作。
在此示例代碼中,如果用戶輸入的數字大於9,則會顯示MessageBox
:
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!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.