Validate TextBox that only allows MS Visual Basic integers? [duplicate]

0

Cordial greeting colleagues, it turns out that I have a form in MS Visual Basic and I'm doing the respective validations to the buttons and text boxes. In text boxes, you must validate that you only receive whole numbers, do not allow decimals or text. To validate that you can not enter text use the following code in the textbox:

Private Sub txtlevantamiento_TextChanged(sender As Object, e As EventArgs) Handles txtlevantamiento.TextChanged
        'Funcion para que solo se pueda escribir numeros en el textbox
        If Not IsNumeric(txtlevantamiento.Text) Then
            txtlevantamiento.Text = " "

        End If

    End Sub

What additional conditional could you add so that you can only write whole numbers and prevent them from writing decimals?

    
asked by Kevin Burbano 30.01.2018 в 16:10
source

1 answer

2

Use the keyPress event of your text box. Attach the following code, which should work, does not allow characters other than numbers and keyboard control keys (delete, delete)

 Private Sub txtlevantamiento_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtlevantamiento.KeyPress
            If Char.IsDigit(e.KeyChar) Then
                e.Handled = False
            ElseIf Char.IsControl(e.KeyChar) Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        End Sub
    
answered by 30.01.2018 / 20:25
source