This is for beginners who have multiple Textboxes and want to restrict the type of characters that can be entered in to each of them.
I wanted this for my project and looked around but could only find examples of restricting numbers to one textbox, which would have meant a lot of repetitive code
i.e TextBox1 only numbers, TextBox2 only Numbers, TextBox3 only Numbers etc
The code below only allows Numbers and the backspace to be entered.
In form Load event
in the body of the codeCode:'Add key press event for each textbox For Each tb In Me.Controls.OfType(Of TextBox)() AddHandler tb.KeyPress, AddressOf keypressed Next
Code:Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs) 'Only allow numbers and backspace to be entered in any of the textboxes If Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then MessageBox.Show("Only numbers 0 - 9 and the backspace can be entered") e.Handled = True End If End Sub




Reply With Quote