Results 1 to 8 of 8

Thread: Restrict what characters can be entered into all textBoxes on a form

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Restrict what characters can be entered into all textBoxes on a form

    vb.net Code:
    1. If Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
    Ooh, no no no.
    vb.net Code:
    1. 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

  3. #3

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: Restrict what characters can be entered into all textBoxes on a form

    Ooh, no no no.

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Restrict what characters can be entered into all textBoxes on a form

    Quote Originally Posted by Mallard8 View Post
    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.

  5. #5

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Restrict what characters can be entered into all textBoxes on a form

    Quote Originally Posted by si_the_geek View Post
    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.

  8. #8

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    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
  •  



Click Here to Expand Forum to Full Width