2012-10-11 46 views

回答

1

這個解決方案,我從這個鏈接(How to allow user to enter only numbers in a textbox in vb.net?

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _ 
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then 
      e.Handled = True 
    End If 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then 
      e.Handled = False 
    End If 
End Sub 
+0

謝謝!有用! – user1699905

1

可以使用KeyPress事件並使用IsNumeric函數來捕獲數字鍵得到。

Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 
    If IsNumeric(e.KeyChar) Then 
     e.Handled = True 
    End If 
End Sub 
1

如果可以,使用MaskedTextBox

由於操作鍵擊可能會導致問題,刪除/退格/複製/粘貼/ ...