2013-03-02 66 views
-1
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" ..VB文本框只接受数字的概率

请帮忙吗?

+1

你去按键事件。 – spajce 2013-03-02 09:25:32

回答

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 
0

它lloks像代码检查文本框中具有值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