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?
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.
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.
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.
Re: How to disallow non-numeric letters in WinForms TextBox?
Quote:
Originally Posted by
si_the_geek
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.
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.
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
Re: How to disallow non-numeric letters in WinForms TextBox?
Quote:
Originally Posted by
gilman
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.
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
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.
Re: How to disallow non-numeric letters in WinForms TextBox?
Quote:
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
Quote:
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
Re: How to disallow non-numeric letters in WinForms TextBox?
Quote:
Originally Posted by
kpmc
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:
e.SuppressKeyPress = If(AllowKeys.Contains(e.KeyCode), False, True)
could be more simply written like this:
vb.net Code:
e.SuppressKeyPress = Not AllowKeys.Contains(e.KeyCode)
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
Re: How to disallow non-numeric letters in WinForms TextBox?
Quote:
Originally Posted by
kpmc
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.