VB.NET - 如何让文本框每次按键只接受一个字符?
VB.NET - How to make textbox accept only one character per keypress?
我的项目是模拟 Enigma Machine,我需要一些处理可视化界面的帮助。
在我的程序中,我需要一个文本框,用户可以在其中输入不限数量的字母。但是,该程序还会根据输入的文本更新其他组件,并为用户显示可视化表示。
例如,当我按下字母 'A' 时,程序将其编码为字母 'S' 并在界面中显示图像。但是,每次对字母进行编码时,该编码都是唯一的。
如您所知,当您在键盘上按住一个字母较长时间时,它会一直写下该字母。例如,如果我一直按下 'A',在我的文本框中我最终会得到 'AAAAAAAAAAAAAAAAAAAAAA',并且它会在对每个 'A'.
进行编码时向图像显示发送垃圾邮件
我的问题是:如何限制文本框每次按键只接受一个字母,但仍然允许两个或三个字母?所以我可以有一个相同字符的子串,只要我通过在键盘上反复按来输入它们。
我需要这个,以便只要按下输入字母,编码字母就会亮起。
您可以使用布尔标志来确定第一个 keypress
事件是否触发,然后取消任何下一个 keypress
直到 KeyUp
事件触发。
Private isKeyHandled As Boolean = False
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox1.KeyPress
e.Handled = Me.isKeyHandled
Me.isKeyHandled = True
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles TextBox1.KeyUp
Me.isKeyHandled = False
End Sub
此外,为了适应,您可以将逻辑添加到子类文本框中:
Public Class TextBoxEx : Inherits TextBox
Public Property DisableKeyRepetition As Boolean = False
Private isKeyHandled As Boolean = False
Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
If Me.DisableKeyRepetition Then
e.Handled = Me.isKeyHandled
Me.isKeyHandled = True
End If
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
MyBase.OnKeyUp(e)
Me.isKeyHandled = False
End Sub
End Class
然后:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TextBoxEx1.DisableKeyRepetition = True
End Sub
如果在 KeyEventArgs 中使用 SuppressKeyPress,则可以控制按键何时完成。在我的示例中,我使用文本框作为要输入的控件。但这也适用于其他控件...
Dim keyIsDown As Boolean 'marked to give status of keyboard key
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If keyIsDown Then 'if key is already down abort
e.SuppressKeyPress = True
Exit Sub
End If
keyIsDown = True 'if key was not already down mark it as down now
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
keyIsDown = False 'key has traveled up clear the marker
End Sub
是不是重复按键,不是按键事件可以查出来的。但它在底层 WM_KEYDOWN message 中可用,lparam 参数的位 #30 将设置为 1。到目前为止,查看该消息的最简单方法是覆盖表单的 ProcessCmdKey() 方法。将此代码粘贴到您的表单中 class:
Private repeatedKey As Boolean
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
repeatedKey = (msg.LParam.ToInt32 And (1 << 30)) <> 0
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
您现在可以在 KeyPressed 事件处理程序中检查此变量。我想你想做这样的事情:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If repeatedKey Then e.Handled = True
If e.KeyChar = ChrW(22) Then e.Handled = True '' Disable paste
If e.KeyChar = ChrW(8) Then e.Handled = False '' Allow repeating backspace
End Sub
第一行完全防止重复键被看到。第二行禁用 copy/pasting,不太可能在您的应用程序中有用。第 3 行仍然允许重复退格键,以便用户可以轻松更正错误。请记住,我只是在猜测您想在这里做什么,这只是一个示例,您可能想要更改它。
我的项目是模拟 Enigma Machine,我需要一些处理可视化界面的帮助。
在我的程序中,我需要一个文本框,用户可以在其中输入不限数量的字母。但是,该程序还会根据输入的文本更新其他组件,并为用户显示可视化表示。
例如,当我按下字母 'A' 时,程序将其编码为字母 'S' 并在界面中显示图像。但是,每次对字母进行编码时,该编码都是唯一的。
如您所知,当您在键盘上按住一个字母较长时间时,它会一直写下该字母。例如,如果我一直按下 'A',在我的文本框中我最终会得到 'AAAAAAAAAAAAAAAAAAAAAA',并且它会在对每个 'A'.
进行编码时向图像显示发送垃圾邮件我的问题是:如何限制文本框每次按键只接受一个字母,但仍然允许两个或三个字母?所以我可以有一个相同字符的子串,只要我通过在键盘上反复按来输入它们。
我需要这个,以便只要按下输入字母,编码字母就会亮起。
您可以使用布尔标志来确定第一个 keypress
事件是否触发,然后取消任何下一个 keypress
直到 KeyUp
事件触发。
Private isKeyHandled As Boolean = False
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox1.KeyPress
e.Handled = Me.isKeyHandled
Me.isKeyHandled = True
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles TextBox1.KeyUp
Me.isKeyHandled = False
End Sub
此外,为了适应,您可以将逻辑添加到子类文本框中:
Public Class TextBoxEx : Inherits TextBox
Public Property DisableKeyRepetition As Boolean = False
Private isKeyHandled As Boolean = False
Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
If Me.DisableKeyRepetition Then
e.Handled = Me.isKeyHandled
Me.isKeyHandled = True
End If
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
MyBase.OnKeyUp(e)
Me.isKeyHandled = False
End Sub
End Class
然后:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TextBoxEx1.DisableKeyRepetition = True
End Sub
如果在 KeyEventArgs 中使用 SuppressKeyPress,则可以控制按键何时完成。在我的示例中,我使用文本框作为要输入的控件。但这也适用于其他控件...
Dim keyIsDown As Boolean 'marked to give status of keyboard key
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If keyIsDown Then 'if key is already down abort
e.SuppressKeyPress = True
Exit Sub
End If
keyIsDown = True 'if key was not already down mark it as down now
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
keyIsDown = False 'key has traveled up clear the marker
End Sub
是不是重复按键,不是按键事件可以查出来的。但它在底层 WM_KEYDOWN message 中可用,lparam 参数的位 #30 将设置为 1。到目前为止,查看该消息的最简单方法是覆盖表单的 ProcessCmdKey() 方法。将此代码粘贴到您的表单中 class:
Private repeatedKey As Boolean
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
repeatedKey = (msg.LParam.ToInt32 And (1 << 30)) <> 0
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
您现在可以在 KeyPressed 事件处理程序中检查此变量。我想你想做这样的事情:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If repeatedKey Then e.Handled = True
If e.KeyChar = ChrW(22) Then e.Handled = True '' Disable paste
If e.KeyChar = ChrW(8) Then e.Handled = False '' Allow repeating backspace
End Sub
第一行完全防止重复键被看到。第二行禁用 copy/pasting,不太可能在您的应用程序中有用。第 3 行仍然允许重复退格键,以便用户可以轻松更正错误。请记住,我只是在猜测您想在这里做什么,这只是一个示例,您可能想要更改它。