简体   繁体   中英

Message boxes not firing on textbox change event

I have a block of c# code on a TextChanged event for a winform textbox. Several of the voids called have messageboxes attached to them so the operator knows if they have valid data. Unfortunately, these calls get skipped completely. I have called the form in question with show() instead of showdialog() to eliminate the form being modal. Still no soap. The event is triggered by a barcode scanner. Code is as follows:

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;
    }
}

I suspect it's a combination of text changed and keypress, but not really sure how it should be triggered properly.

After a week and a half, I have the answer, tested and verified. TaW and BillRuhl were on the right track with Leave and KeyPress. When those did not work, I finally hit on the KeyUp event.

A little background. A generic keyboard wedge USB scanner automatically adds a carriage return that trimming "\\r\\n" or Environment.Newline() will not get rid of. After several tries using different combinations and keypress, I caught the app firing a form before closing it. The barcode scanner, unlike normal keyboard input or cutting and pasting, continues to send an enter key to anything that listens for it during an event. I know. Buggy. But if we fire on the keyup event instead, like so...

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;
               }
           }
       }
   }

...We look only for an enter key, validate on the length of the string (In this case, "==12" is essential for validation), and use KeyEventArgs to filter out Keys.Enter. Works perfectly with one caveat. KeyUp actually works on the form level, so it will fire on other text boxes as well. In this case, txtScanCode is the only one that has data-bound functions behind it, so all the validation is written to check against that control.

Thanks all for chiming in. I think we broke Google a couple of times trying to figure it out.

I just did a copy/paste test, and I think the problem may be in your if condition. If I copy more than 12 characters and paste it into the text box, the 'if' statement doesn't trigger.

This simple change seemed to fix that case:

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)
}

Try a different event...I have better luck with the Leave event than I do with the TextChanged event. So your method would look like:

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;
 }
}

Don't forget to wire up the Leave event...

Hope that helps Bill

Just put MessageBox.Show("ble"); and then MessageBox.Show("blu); and the "blu" will fire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM