VB.net中限制TextBox只能输入数字(小数、负数)

 

 

Private Sub TextBox1_KeyPress(ByVal sender As Object,
                                                   ByVal e As System.Windows.Forms.KeyPressEventArgs)
                     Handles Textbox1.KeyPress,Textbox2.KeyPress,Textbox3.KeyPress
 

    '判断输入的是否是数字、小数点或“BackSpace”符号

    If Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8) Then

         '判断输入的小数点是否唯一
          If e.KeyChar = "." And InStr(CType(sender, TextBox).Text, ".") > 0 Then
               e.Handled = True
          Else
               e.Handled = False
          End If

     '判断输入的负号是否是在第一位
     ElseIf e.KeyChar = "-" And CType(sender, TextBox).Text = "" Then
          e.Handled = False
     Else
          e.Handled = True
     End If
End Sub
 

你可能感兴趣的:(.net,windows,vb,VB.NET)