|
-
Mar 14th, 2019, 05:06 AM
#1
Thread Starter
Addicted Member
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?
-
Mar 14th, 2019, 05:14 AM
#2
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.
-
Mar 14th, 2019, 05:26 AM
#3
Thread Starter
Addicted Member
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.
-
Mar 14th, 2019, 05:42 AM
#4
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.
-
Mar 14th, 2019, 06:12 AM
#5
Re: How to disallow non-numeric letters in WinForms TextBox?
 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.
-
Mar 14th, 2019, 10:33 AM
#6
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
 
-
Mar 15th, 2019, 04:12 AM
#7
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
-
Mar 15th, 2019, 04:23 AM
#8
Re: How to disallow non-numeric letters in WinForms TextBox?
 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.
-
Mar 15th, 2019, 09:22 AM
#9
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.
-
Mar 15th, 2019, 10:37 AM
#10
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
 
-
Mar 15th, 2019, 10:49 AM
#11
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
-
Mar 15th, 2019, 08:10 PM
#12
Re: How to disallow non-numeric letters in WinForms TextBox?
 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)
-
Mar 15th, 2019, 10:08 PM
#13
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
-
Mar 15th, 2019, 10:49 PM
#14
Re: How to disallow non-numeric letters in WinForms TextBox?
 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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|