Results 1 to 14 of 14

Thread: How to disallow non-numeric letters in WinForms TextBox?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    How to disallow non-numeric letters in WinForms TextBox?

    I am reworking old VB6 application into VB.NET.

    I have a form with 160 TextBox fileds kept inside TextBoxArray component.

    MaksedTextBox can be easily set to allow only numeric letters from keyboard, but text box component doesn't have that ability.

    I have two solutions, either replace all 160 fields with 160 MaskedTextBox fields or write some script and place it inside the keyup event and remove non-numeric letters.

    The TextBoxArray has KeyUp, KeyDown, KeyPress events but I am not sure how to format text to be just numeric in VB.NET or either in VB6 compatibility.
    Any ideas how to disable non-numeric letters or either remove those?

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

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Instead of a Textbox or MaskedTextBox, use a NumericUpDown.

    It is basically a textbox that is designed for numbers (and doesn't allow anything else, including if you paste). It can limit the range of numbers (eg: must be between 1 and 7), and has optional up/down arrows too so that the user can click rather than type.

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Well if it is like a numeric picker component, that wouldn't be good since it is a data entry application and entering data should be fast.

    And also replacing 160 TextBoxes is also not a fast solution.

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

    Re: How to disallow non-numeric letters in WinForms TextBox?

    It isn't slower for the user because it can be used in the same way as a textbox, the buttons (if you choose to allow them) are an extra feature that the user can also use if they want - but they can still type if that is what they want.


    As for replacing them all, that is probably easier than you think... just change one, then close the project, and open the files for the form in NotePad (if there is a formname.Designer.vb , that would be the best to look at).

    Make a backup copy of the file, then you should be able to do a manual replace in Notepad of each control... it would probably take about 5 to 10 minutes.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Quote Originally Posted by si_the_geek View Post
    As for replacing them all, that is probably easier than you think... just change one, then close the project, and open the files for the form in NotePad (if there is a formname.Designer.vb , that would be the best to look at).

    Make a backup copy of the file, then you should be able to do a manual replace in Notepad of each control... it would probably take about 5 to 10 minutes.
    There's no need to do it in Notepad. Find & Replace is easier in VS. Click the Show All Files button in the Solution Explorer, expand the form, double-click the designer file, hit Ctrl+F and away you go.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to disallow non-numeric letters in WinForms TextBox?

    There is one other point to keep in mind with NumericUpDown controls: They have a Text property, just like a Textbox, but you REALLY don't want to be using that. Instead, you want to be using the .Value property. Therefore, once you have changed the textboxes, you will also want to change any place that worked with the .Text property of those controls.

    Some events may have to change, too.
    My usual boring signature: Nothing

  7. #7
    Hyperactive Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    273

    Re: How to disallow non-numeric letters in WinForms TextBox?

    You can use this code for the textbox
    Code:
        Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
            Select Case e.KeyCode
                Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9
                Case Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4, _
                     Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9
                Case Else
                    e.SuppressKeyPress = True
            End Select
        End Sub

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Quote Originally Posted by gilman View Post
    You can use this code for the textbox
    Code:
        Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
            Select Case e.KeyCode
                Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9
                Case Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4, _
                     Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9
                Case Else
                    e.SuppressKeyPress = True
            End Select
        End Sub
    Try holding down the Shift key while pressing the digit keys and see how well that code works. Use KeyData rather than KeyCode.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: How to disallow non-numeric letters in WinForms TextBox?

    This works ok
    Code:
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            Dim AllowKeys() As Keys = {Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4,
                                       Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9, Keys.Back}       
            e.SuppressKeyPress = If(AllowKeys.Contains(e.KeyCode), False, True)
        End Sub
    Last edited by kpmc; Mar 15th, 2019 at 09:27 AM.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to disallow non-numeric letters in WinForms TextBox?

    How does it do with copy/paste? That's usually what breaks things like this.

    It's a bunch of contortion to get around using the control that was designed to solve this particular problem, and for no clear reason.
    My usual boring signature: Nothing

  11. #11
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: How to disallow non-numeric letters in WinForms TextBox?

    It's a bunch of contortion to get around using the control that was designed to solve this particular problem, and for no clear reason.
    I completely agree with you 100% I personally use the nud control for this purpose, and I will admit I have not read through fully to see why poster does not want to use it, or why this thread made it to post#11

    How does it do with copy/paste? That's usually what breaks things like this.
    You cant do either as the only keys that are not suppressed are the keys in the array. I have tested and I could not produce an undesired result using any key or any modifier/key combination

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Quote Originally Posted by kpmc View Post
    This works ok
    Code:
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            Dim AllowKeys() As Keys = {Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4,
                                       Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9, Keys.Back}       
            e.SuppressKeyPress = If(AllowKeys.Contains(e.KeyCode), False, True)
        End Sub
    If you consider being able to use the number pad only to be OK. How about if someone is using a keyboard without one? Also, this:
    vb.net Code:
    1. e.SuppressKeyPress = If(AllowKeys.Contains(e.KeyCode), False, True)
    could be more simply written like this:
    vb.net Code:
    1. e.SuppressKeyPress = Not AllowKeys.Contains(e.KeyCode)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: How to disallow non-numeric letters in WinForms TextBox?

    You could throw in a condition for your modifier keys if you want to use the other numbers
    Code:
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            Dim AllowKeys() As Keys = {Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4,
                                       Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9,
                                       Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.Back}
    
            Dim SuppressKeys As Boolean = If(AllowKeys.Contains(e.KeyCode), False, True)
            e.SuppressKeyPress = Not AllowKeys.Contains(e.KeyCode) OrElse e.Modifiers = Keys.Shift
        End Sub

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to disallow non-numeric letters in WinForms TextBox?

    Quote Originally Posted by kpmc View Post
    You could throw in a condition for your modifier keys if you want to use the other numbers
    Or you could do as I said in post #8 and use KeyData instead of KeyCode. KeyData is the combination of current modifier keys and the key that was just depressed so if that is equal to just a digit or number pad key then you know that no modifier keys were depressed. That will then also exclude keys depressed with Ctrl, Alt or Ctrl+Alt as well.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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