为什么MessageBox对SuppressKeyPress有影响? [英] Why does MessageBox has an affect on SuppressKeyPress?

查看:98
本文介绍了为什么MessageBox对SuppressKeyPress有影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是vb中的一种特殊情况.我在 SuppressKeyPress 属性,我发现了一些奇怪的地方.

Here is a peculiar situation in vb. I was messing the with SuppressKeyPress property and I found out something strange.

情况

让我们说我有一个名为txtName的文本框,我希望该名称不包含任何数字,并且在插入数字时,将弹出一个MessageBox并报告错误.

Lets say I have a text box called txtName, and I want the name to be without any numbers, and when a number is inserted, a MessageBox will pop out and report an error.

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
    If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 And _
e.Modifiers <> Keys.Shift Then
        e.SuppressKeyPress = True
        MsgBox("Error - A Number has been pressed")
        'The number appeared in the text box.
    End If
End Sub

在这种情况下,由于某些奇怪的原因,如果我键入数字,尽管我压下了按键,但仍将它写在文本框中.

In this case, for some strange reason, if I type a number, it will be written in the text box, although I suppressed the keypress.

我发现,如果我删除MsgBox行,则该数字将不会出现在文本框中.

What I found out is that if I remove the MsgBox line, the number will not appear in the text box.

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
    If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 And _
e.Modifiers <> Keys.Shift Then
        e.SuppressKeyPress = True
        'The number did not appear in the text box.
    End If
End Sub

问题

这是怎么回事?为什么MsgBox允许"按下该键?为什么它对SuppressKeyPress属性有影响?

What is going on? Why the MsgBox "allows" the key to be pressed? Why it has any effect on the SuppressKeyPress property?

推荐答案

这是使用MessageBox的非常典型的副作用,它可能会导致很多棘手的问题诊断. SuppressKeyPress 属性是通过在消息队列中搜索任何按键事件并将其删除来实现的.但这只能在事件处理程序完成后 发生.

This is a pretty typical side-effect of using MessageBox, it can cause lots of tricky to diagnose problems. The SuppressKeyPress property is implemented by searching the message queue for any keypress events and removing them. But that can happen only after your event handler completes.

问题是,它没有很快完成.您的MsgBox()调用即将接管,它本身开始启动消息循环.就像对话框一样,相当于调用臭名昭著的DoEvents()方法.它将很容易地在消息队列中分派待处理的消息,包括那些本应被抑制的按键消息.

Trouble is, it isn't completing any time soon. Your MsgBox() call is taking over and it starts pumping a message loop by itself. Like dialogs do, the equivalent of calling the infamous DoEvents() method. And it will readily dispatch the pending messages in the message queue, including those keypress messages that were supposed to be suppressed.

此问题的临时帮助是在事件处理完成且Winforms有机会清除按键消息后,显示消息框以后.通过使用Control.BeginInvoke()方法可以完美完成:

A band-aid for such a problem is to display the message box later, after the event handling is completed and Winforms had a chance to purge the keypress messages. Elegantly done by using the Control.BeginInvoke() method:

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 And e.Modifiers <> Keys.Shift Then
        e.SuppressKeyPress = True
        Me.BeginInvoke(New Action(Sub() MsgBox("Error - A Number has been pressed")))
    End If
End Sub

但是 real 修复方法是使用正确的事件.您应该始终使用KeyPress事件来进行这种过滤.这也避免了用户在使用KeyDown时始终存在的对用户活动键盘布局的非常痛苦的依赖.修复:

But the real fix is to use the correct event. You should always use the KeyPress event instead for this kind of filtering. That also avoids the very painful dependency on the user's active keyboard layout that's always present when you use KeyDown. Fix:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar >= "0"c AndAlso e.KeyChar <= "9"c Then
        e.Handled = True
        MsgBox("I don't like digits")
    End If
End Sub

但是再说一次,不要使用消息框来拍打用户,因为这是一个简单的错误.

But then again, don't use message boxes to slap the user for making a simple mistake.

这篇关于为什么MessageBox对SuppressKeyPress有影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆