How to filter textbox input to numeric only?

前端 未结 9 1513
耶瑟儿~
耶瑟儿~ 2020-12-21 13:37

How do I suppress all data except numeric?

This is not working on KeyDown():

If e.KeyData < Keys.D0 Or e.KeyData > Keys.D9 Then
          


        
9条回答
  •  情歌与酒
    2020-12-21 14:14

    This code will help you to restrict multiple TEXTBOX to accept only NUMERIC VALUE and BACKSPACE key. However you can remove If e.KeyChar <> ChrW(Keys.Back) Then and End If value from code when you don't want to accept backspace key. Enhanced version of the kevchadders solution in this thread.

    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then
            Else
                e.Handled = True
            End If
        End If
    End Sub
    

提交回复
热议问题