En esta pequeña entrada vamos a ver el código para que un componente del tipo TextBox de Visual Studio solo acepte números.
Para ello crearemos el evento Key_Press del componente e introducimos el siguiente código:
If e.KeyChar.IsDigit(e.KeyChar) Then e.Handled = False ElseIf e.KeyChar.IsControl(e.KeyChar) Then e.Handled = False Else e.Handled = True End If
por lo que nos quedará de ésta forma:
Private Sub miTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles miTextBox.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