簡體   English   中英

消息框不觸發文本框更改事件

[英]Message boxes not firing on textbox change event

我在一個 Winform 文本框的 TextChanged 事件上有一段 c# 代碼。 幾個被調用的空洞附有消息框,以便操作員知道它們是否有有效數據。 不幸的是,這些調用被完全跳過了。 我用 show() 而不是 showdialog() 調用了有問題的表單,以消除表單是模態的。 還是沒有肥皂。 該事件由條形碼掃描儀觸發。 代碼如下:

private void txtScanCode_TextChanged(object sender, EventArgs e)
{
    string barCode;
    barCode = txtScanCode.Text;

    if (txtScanCode.Text.Length == 12)
    {
        MessageBox.Show(this, "Hey, look!", "A message box!", 
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        FindScanItem(barCode);
        barCode = "";
        txtScanCode.SelectionStart = 0;
        txtScanCode.SelectionLength = txtScanCode.Text.Length;
    }
}

我懷疑這是文本更改和按鍵的組合,但不確定應該如何正確觸發。

一個半星期后,我有了答案,經過測試和驗證。 TaW 和 BillRuhl 在 Leave 和 KeyPress 方面走在正確的軌道上。 當這些都不起作用時,我終於找到了 KeyUp 事件。

一點背景。 通用鍵盤楔形 USB 掃描儀會自動添加回車符,修剪 "\\r\\n" 或 Environment.Newline() 不會刪除。 在多次嘗試使用不同的組合和按鍵之后,我發現應用程序在關閉之前觸發了一個表單。 與普通鍵盤輸入或剪切和粘貼不同,條碼掃描器會繼續向事件期間偵聽它的任何內容發送回車鍵。 我知道。 越野車。 但是如果我們改為觸發 keyup 事件,就像這樣......

private void txtScanCode_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
   {

       if (e.KeyCode == Keys.Enter)
       {
           e.SuppressKeyPress = true;

           barCode = txtScanCode.Text.Trim().ToString();
           if (!doDataStuff) //This boolean is instantiated as false
           {
               if (txtScanCode.Text.Length == 12)
               {
                   doDataStuff = true; //boolean tells the app go run data functions.
                   MessageBox.Show(this, "Pop up worked!", "Cool!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   getData(barCode); //Data methods performed on the barcode
                   barCode = "";
                   txtScanCode.Focus();
                   txtScanCode.SelectionStart = 0;
                   txtScanCode.SelectionLength = txtScanCode.Text.Length;
               }
           }
       }
   }

...我們只查找回車鍵,驗證字符串的長度(在這種情況下,“==12”對於驗證至關重要),並使用 KeyEventArgs 過濾掉 Keys.Enter。 只需一個警告即可完美運行。 KeyUp 實際上適用於表單級別,因此它也會在其他文本框上觸發。 在這種情況下,txtScanCode 是唯一一個在其背后具有數據綁定函數的函數,因此所有驗證都被寫入以檢查該控件。

感謝大家的參與。我想我們在嘗試解決問題的過程中破壞了 Google 幾次。

我剛剛做了一個復制/粘貼測試,我認為問題可能出在你的if條件下。 如果我復制超過 12 個字符並將其粘貼到文本框中,則不會觸發“if”語句。

這個簡單的改變似乎解決了這個問題:

if (textBox1.Text.Length >= 12)
{
    MessageBox.Show(this, "Hey, look!", "A message box!",
        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    // the rest of your code here
    // (you may want to do some additional validation 
    // on the text if it's more than 12 characters)
}

嘗試不同的事件...離開事件的運氣比使用 TextChanged 事件的好。 所以你的方法看起來像:

private void txtScanCode_Leave(object sender, EventArgs e)
{
string barCode;
barCode = txtScanCode.Text;

 if (txtScanCode.Text.Length == 12)
 {
    MessageBox.Show(this, "Hey, look!", "A message box!", 
        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    FindScanItem(barCode);
    barCode = "";
    txtScanCode.SelectionStart = 0;
    txtScanCode.SelectionLength = txtScanCode.Text.Length;
 }
}

不要忘記連接 Leave 事件...

希望能幫助比爾

只要把MessageBox.Show("ble"); 然后MessageBox.Show("blu);"blu"將觸發。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM