Restrict what characters can be entered into all textBoxes on a form
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
Code:
'Add key press event for each textbox
For Each tb In Me.Controls.OfType(Of TextBox)()
AddHandler tb.KeyPress, AddressOf keypressed
Next
in the body of the code
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
Re: Restrict what characters can be entered into all textBoxes on a form
vb.net Code:
If Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
Ooh, no no no.
vb.net Code:
If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then
Char.IsControl will allow Delete and other control characters as well as Backspace, but any that the user can enter should be allowed anyway.
Re: Restrict what characters can be entered into all textBoxes on a form
:D
Just when I think I'm being helpful!
At least any beginners can read both post or the mods can delete it. Whichever
Re: Restrict what characters can be entered into all textBoxes on a form
Quote:
Originally Posted by
Mallard8
Just when I think I'm being helpful!
You were, so there was nothing wrong with posting. Plenty of my CodeBank posts, as well as other posts, could almost certainly be improved upon. That especially goes for those I wrote when I had less experience than I do now.
Re: Restrict what characters can be entered into all textBoxes on a form
Thing is john when I removed my line of code and replaced it with yours I was able to place letters in the textbox?
So have I misunderstood what you are trying to show in the code you posted?
Re: Restrict what characters can be entered into all textBoxes on a form
I think the AndAlso should have been OrElse
...or to arguably make it more readable, use two Not's instead:
Code:
If Not (Char.IsDigit(e.KeyChar)) AndAlso Not (Char.IsControl(e.KeyChar)) Then
Re: Restrict what characters can be entered into all textBoxes on a form
Quote:
Originally Posted by
si_the_geek
I think the
AndAlso should have been
OrElse
...or to arguably make it more readable, use two Not's instead:
Code:
If Not (Char.IsDigit(e.KeyChar)) AndAlso Not (Char.IsControl(e.KeyChar)) Then
You're correct. I started out with that and then edited it but forgot to change the AndAlso to OrElse. I've edited my original post.
Re: Restrict what characters can be entered into all textBoxes on a form
When I can take the stone from the hand of the master my training will be complete. :rolleyes: