我发现很多方法可以限制用户输入字母字符,但我希望更具体。我正在制作一个二进制计算器,我希望用户只能填写数字'0'和'1'。
答案 0 :(得分:1)
虽然有很多方法可以做到这一点,但我喜欢这种方法:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim valid_values As String = "01"
If Not valid_values.Contains(e.KeyChar) Then
e.Handled = True
End If
End Sub
答案 1 :(得分:0)
这将确保按下的键为0或1.否则它将表现为没有按下任何键。
Private Sub txtBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBox.KeyDown
If Not (e.KeyCode = Keys.0 OR e.KeyCode = Keys.1) Then
e.Handled = True
EndIf
End Sub
答案 2 :(得分:0)
应用于textbox_keypress事件..
Private Sub txtbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox.KeyPress
Dim k As Byte = Asc(e.KeyChar)
If Not (e.KeyChar = "0" Or e.KeyChar = "1" Or k = 8 Or k = 13) Then e.Handled = True
End Sub