处理按键事件vb.net/整个表格 [英] Handles key press event vb.net / entire form

查看:74
本文介绍了处理按键事件vb.net/整个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们

我找到了一种方法来处理文本框上的各个按键,但是这种方式下,我必须自己编写每个文本框的代码.
我想知道如何为表单上的所有文本框设置规则/按键事件.
因此,当按下键"时,什么都不做.
使用字符"保存数据时出现错误 我希望这是有道理的...:)

Hi Guys

I have found a way to handle individual key press on textboxes but this way I have to code each textbox on its own.
I would like to know how to set a rule / Key Press event for all textboxes on the form.
So when the Key: " ‘ " is pressed do nothing.
Im getting errors when saving data using the character " ‘ "
I hope this makes sense... : )

推荐答案

您可以处理Form事件
You can handle a Form event KeyPress[^] at the Form Level.
Sub Form1_KeyPress(ByVal sender As Object, _
    ByVal e As KeyPressEventArgs) Handles Me.KeyPress

    If e.KeyChar >= ChrW(48) And e.KeyChar <= ChrW(57) Then
        MessageBox.Show(("Form.KeyPress: '" + _
            e.KeyChar.ToString() + "' pressed."))

        Select Case e.KeyChar
            Case ChrW(49), ChrW(52), ChrW(55)
                MessageBox.Show(("Form.KeyPress: '" + _
                    e.KeyChar.ToString() + "' consumed."))
                e.Handled = True 
        End Select 
    End If 
End Sub


此答案只是
如何:在表单级别处理键盘输入 [ ^ ]


希望对您有所帮助.!
--Amit


This answer is just a reference of How to: Handle Keyboard Input at the Form Level[^]


Hope it helps.!
--Amit


Ami,我可以使用它:

将表单的KeyPreview属性设置为true

我的代码..

Ami I got it to work:

set the KeyPreview property of the form to true

My Code..

Sub Form1_KeyPress(ByVal sender As Object, _
    ByVal e As KeyPressEventArgs) Handles Me.KeyPress

    If e.KeyChar = ChrW(39) Then
        MessageBox.Show(("Form.KeyPress: '" + _
            e.KeyChar.ToString() + "' pressed."))

        Select Case e.KeyChar
            Case ChrW(39)
                MessageBox.Show(("Form.KeyPress: '" + _
                    e.KeyChar.ToString() + "' consumed."))
                e.Handled = True
        End Select
    End If
End Sub



如果结束
结束子

如果结束
结束子



非常感谢您的帮助

干杯



End If
End Sub

End If
End Sub



Thank you so much for the help

Cheers


这篇关于处理按键事件vb.net/整个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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