Private Sub TextBox1_textChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim intValue As Integer
If Not Integer.TryParse(TextBox1.Text, intValue) OrElse intValue < 1 OrElse intValue > 10 Then
TextBox1.Text = ""
Else
MsgBox(intValue)
End If
End Sub
这是我的代码,当我按"1"
textbox1.text = "1"
时,文本框已经接受了数字,但是当我按下"1"
第一个"1"
时覆盖..当我按"2"
时,textbox1.text现在只等于"2"
..
请帮忙吗?
答案 0 :(得分:1)
更好地处理KeyPress
事件以实现所需的功能
Private Sub DigitTextBox_KeyPress(sender As Object, e As KeyPressEventArgs)
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
'Just Digits
If e.KeyChar = ChrW(8) Then
e.Handled = False
End If
'Allow Backspace
End Sub
答案 1 :(得分:0)
看起来代码正在检查文本框的值是否为1-10。如果是这样,这将起作用
Dim intValue As Integer
If Integer.TryParse(TextBox1.Text, intValue) AndAlso intValue > 0 AndAlso intValue < 11 Then
Debug.WriteLine(intValue) 'good value
Else
TextBox1.Text = "" 'bad value
End If