![](/img/trans.png)
[英]Application_end event doesn't update the database when I close visual studio
[英]MessageBox doesn't close when I use KeyUp event
我的表單有一個文本框用於輸入,列表視圖用於保存。
用戶在文本框中輸入數字,然后輸入回車,程序檢查輸入數字的長度和重復
文本框KeyUp事件
private void txb_MList_num_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (check_MList_dup()) { lstv_MList.Items.Add(txb_MList_num.Text); allList.Add(txb_MList_num.Text); txb_MList_num.Text = ""; } } }
check_MList_dup()
private bool check_MList_dup() { bool OK = true; if (txb_MList_num.TextLength < 11) { MessageBox.Show("Input more text(length = 11)"); return false; } else { for (int i = 0; i < allList.Count; i++) if (allList[i].Equals(txb_MList_num.Text)) { MessageBox.Show("It's duplication."); return false; } } return OK; }
但是在使用鼠標之前,用戶輸入要輸入以關閉MessageBox,再次編程顯示MessageBox。
我使用斷點調試它,顯示MessageBox時事件不發生。
但是刪除斷點后,MessageBox被重復。
我使用e.KeyCode == Keys.Enter && this.Focused
但是this.Focused
總是返回false
。
如何關閉MessageBox?
您可以使用PreviewKeyDown :
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
// Do your staff here...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.