2013-03-02 67 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