-
Apr 10th, 2020, 08:27 AM
#1
Thread Starter
Hyperactive Member
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
Learning is a never ending subject.
-
Apr 10th, 2020, 08:41 AM
#2
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.
Last edited by jmcilhinney; Apr 10th, 2020 at 11:19 AM.
Reason: Changed AndAlso to OrElse
-
Apr 10th, 2020, 08:45 AM
#3
Thread Starter
Hyperactive Member
Re: Restrict what characters can be entered into all textBoxes on a form
Just when I think I'm being helpful!
At least any beginners can read both post or the mods can delete it. Whichever
Learning is a never ending subject.
-
Apr 10th, 2020, 09:34 AM
#4
Re: Restrict what characters can be entered into all textBoxes on a form
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.
-
Apr 10th, 2020, 10:59 AM
#5
Thread Starter
Hyperactive Member
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?
Learning is a never ending subject.
-
Apr 10th, 2020, 11:11 AM
#6
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
-
Apr 10th, 2020, 11:19 AM
#7
Re: Restrict what characters can be entered into all textBoxes on a form
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.
-
Apr 10th, 2020, 01:43 PM
#8
Thread Starter
Hyperactive Member
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.
Learning is a never ending subject.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|