简体   繁体   English

消息框不触发文本框更改事件

[英]Message boxes not firing on textbox change event

I have a block of c# code on a TextChanged event for a winform textbox.我在一个 Winform 文本框的 TextChanged 事件上有一段 c# 代码。 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.我用 show() 而不是 showdialog() 调用了有问题的表单,以消除表单是模态的。 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. TaW 和 BillRuhl 在 Leave 和 KeyPress 方面走在正确的轨道上。 When those did not work, I finally hit on the KeyUp event.当这些都不起作用时,我终于找到了 KeyUp 事件。

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.通用键盘楔形 USB 扫描仪会自动添加回车符,修剪 "\\r\\n" 或 Environment.Newline() 不会删除。 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...但是如果我们改为触发 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;
               }
           }
       }
   }

...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. ...我们只查找回车键,验证字符串的长度(在这种情况下,“==12”对于验证至关重要),并使用 KeyEventArgs 过滤掉 Keys.Enter。 Works perfectly with one caveat.只需一个警告即可完美运行。 KeyUp actually works on the form level, so it will fire on other text boxes as well. KeyUp 实际上适用于表单级别,因此它也会在其他文本框上触发。 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.在这种情况下,txtScanCode 是唯一一个在其背后具有数据绑定函数的函数,因此所有验证都被写入以检查该控件。

Thanks all for chiming in. I think we broke Google a couple of times trying to figure it out.感谢大家的参与。我想我们在尝试解决问题的过程中破坏了 Google 几次。

I just did a copy/paste test, and I think the problem may be in your if condition.我刚刚做了一个复制/粘贴测试,我认为问题可能出在你的if条件下。 If I copy more than 12 characters and paste it into the text box, the 'if' statement doesn't trigger.如果我复制超过 12 个字符并将其粘贴到文本框中,则不会触发“if”语句。

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.尝试不同的事件...离开事件的运气比使用 TextChanged 事件的好。 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...不要忘记连接 Leave 事件...

Hope that helps Bill希望能帮助比尔

Just put MessageBox.Show("ble");只要把MessageBox.Show("ble"); and then MessageBox.Show("blu); and the "blu" will fire.然后MessageBox.Show("blu);"blu"将触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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