VB.NET - 如何使每个按键只接受一个字符的文本框?

时间:2015-04-17 12:15:01

标签: vb.net keyboard richtextbox

我的项目是模拟Enigma机器,我需要一些处理可视界面的帮助。

在我的程序中,我需要一个文本框,用户可以在其中输入无限数量的字母。但是,该程序还会根据输入的文本更新其他组件,并为用户显示可视化表示。

例如,当我按下字母“A'该程序编码说,字母' S'并在界面中显示图像。但是,每次编码字母时,该编码都是唯一的。

如你所知,当你长时间按下键盘上的字母时,它会继续写这封信。例如,如果我保持' A'按下,在我的文本框中,我最终得到了AAAAAAAAAAAAAAAAAAAAAA'并且当它编码每个单独的A'时,它会对图像显示进行搜索。

我的问题是:如何限制文本框每个按键只接受一个字母,但仍然允许双字母或三字母?所以我可以有一个相同字符的子串,只要我通过在键盘上反复按下来输入它们。

我需要这样,所以只要按下输入字母,编码的灯就会亮起。

3 个答案:

答案 0 :(得分:3)

它是否是重复键不是你可以在关键事件中找到的东西。但它在底层WM_KEYDOWN message中可用,lparam参数的第30位将被设置为1.到目前为止,查看该消息的最简单方法是覆盖表单的ProcessCmdKey()方法。将此代码粘贴到表单类中:

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

第1行可防止重复键被发现。第二行禁用复制/粘贴,在您的应用中不太可能有用。第3行仍然允许重复退格键,以便用户可以轻松纠正错误。请记住,我只是猜测你想在这里做什么,这只是一个例子,你可能想要改变它。

答案 1 :(得分:2)

您可以使用布尔标记来确定是否已触发第一个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

此外,为了适应您的需要,可以将逻辑添加到子类TextBox中:

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

答案 2 :(得分:2)

如果在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