-
Jul 1st, 2021, 08:26 PM
#1
Thread Starter
Lively Member
Can't Parse textbox.
I'm trying to restrict the input of a text box to allow only numbers (0-9). I intercept "KeyDown" and update Textbox1.Text only if a number is typed:
(Updated)
----------
Code:
Public Class Form1
Dim strPreviousID As String = ""
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown
If e.KeyValue > 95 And e.KeyValue < 106 Then ' Only ASCII values from "0" to "9".
strPreviousID += Chr(e.KeyValue - 48) ' ASCII value is Keyboard - 48
Else
TextBox1.Text = strPreviousID
End If
strPreviousID = TextBox1.Text
End Sub
End Class
----------
PROBLEM (Updated): If the very first character is not a number, it STILL appears in the text box. If you type anything other than a number, the box contents are replaced with that one letter.
For the life of me, I can not figure out how to stop non-numeric values from appearing.
All "solutions" found online either don't work with VB.Net or are wildly unnecessarily complex.
So instead of "KeyDown", I tried "TextChanged":
Code:
Public Class Form1
Dim strPreviousID As String = ""
Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles Textbox1.TextChanged
If Textbox1.Text.Last.ToString >= "0" And Textbox1.Text.Last.ToString <= "9" Then ' Only "0" thru "9".
strPreviousID += Textbox1.Text.Last.ToString
Else
Textbox1.Text = strPreviousID
End If
Textbox1.Text = strPreviousID
End Sub
End Class
This comes with its own set of problem. Numbers work fine, but "If" line throws an exception if anything other than a number is typed.
I'm stumped. TIA
Last edited by Mugsy323; Jul 1st, 2021 at 10:27 PM.
-
Jul 1st, 2021, 08:32 PM
#2
Re: Can't Parse textbox.
I suggest that you follow the CodeBank link in my signature and check out my thread on creating a Numeric TextBox. You can use what I provided as is or you can just take the bits you want to use in your own code.
-
Jul 1st, 2021, 09:05 PM
#3
Re: Can't Parse textbox.
I'm trying to restrict the input of a text box to allow only numbers (0-9). I intercept "KeyDown" and update Textbox1.Text only if a number is typed:
Why not use the NumericUpDown control?
-
Jul 1st, 2021, 10:23 PM
#4
Thread Starter
Lively Member
Re: Can't Parse textbox.
Originally Posted by wes4dbt
Why not use the NumericUpDown control?
Thx for the suggestion, but users can use any number. Even add leading zeros if desired. I don't want them to select a fixed length numeric value using four or five drop-downs, and one long list is unwieldy.
-
Jul 1st, 2021, 10:24 PM
#5
Thread Starter
Lively Member
Re: Can't Parse textbox.
Originally Posted by jmcilhinney
I suggest that you follow the CodeBank link in my signature and check out my thread on creating a Numeric TextBox. You can use what I provided as is or you can just take the bits you want to use in your own code.
Thx. Which of your code examples has what I'm looking for?
-
Jul 1st, 2021, 10:28 PM
#6
Re: Can't Parse textbox.
Originally Posted by Mugsy323
Thx for the suggestion, but users can use any number. Even add leading zeros if desired. I don't want them to select a fixed length numeric value using four or five drop-downs, and one long list is unwieldy.
That reply suggests that you don't know how a NumericUpDown works. It may still not be perfectly suitable for your specific scenario but it doesn't work the way you seem to think it does.
Last edited by jmcilhinney; Jul 1st, 2021 at 10:33 PM.
-
Jul 1st, 2021, 10:35 PM
#7
Re: Can't Parse textbox.
Originally Posted by Mugsy323
Thx. Which of your code examples has what I'm looking for?
Make an effort to find it for yourself. You have keywords, all my threads are descriptively-named, there's only 70 of them and you have a browser with a Find function. I'm not here to spoon-feed when I would have to go and look for it the very same way you would.
-
Jul 2nd, 2021, 05:44 AM
#8
Thread Starter
Lively Member
Re: Can't Parse textbox.
Originally Posted by jmcilhinney
Make an effort to find it for yourself. You have keywords, all my threads are descriptively-named, there's only 70 of them
Only 70. SMH
The first result searching on "numeric textbox" appears to be the sample in question. If this is the one you meant, it took 2 seconds to find. You could have easily posted the link rather than make me search for it.
And it doesn't work.
First, it uses a bunch of vb constants that VS-2019 appears to no longer supports ("vbKey0", "vbKey9", "vbKeyBack", etc) and must be replaced. And second, it does not validate your typing in real time. It only parses the text once a button is clicked. I can already do that. That's not what I need.
The bug I'm trying to solve is created by the fact I'm trying to parse in real-time. The moment I do that, your code no longer works.
If I'm looking at the wrong sample, please provide the correct link rather than having me search and test a dozen variations.
Last edited by Mugsy323; Jul 2nd, 2021 at 06:10 AM.
-
Jul 2nd, 2021, 05:48 AM
#9
Re: Can't Parse textbox.
Originally Posted by Mugsy323
Only 70.
Yes, only 70. You only have to skim the title of each in a vertical list to see whether it relates to the specified topic. How long could that take? A minute or two at the most. Hardly onerous.
-
Jul 2nd, 2021, 06:00 AM
#10
Re: Can't Parse textbox.
Why not use the keypress event and e.handled=true to suppress the keys you do not want?
Also you want to remember to allow the backspace key.
-
Jul 2nd, 2021, 06:28 AM
#11
Thread Starter
Lively Member
Re: Can't Parse textbox.
Originally Posted by DataMiser
Why not use the keypress event and e.handled=true to suppress the keys you do not want?
Also you want to remember to allow the backspace key.
Nice. I didn't know that was an option. Everything I tried was going to the Textbox window first.
I did it with two lines of code:
Code:
If e.KeyChar.ToString < "0" Or e.KeyChar.ToString > "9" Then e.Handled = True
If e.KeyChar.ToString = vbBack Then e.Handled = False
Big thanks.
-
Jul 2nd, 2021, 08:27 AM
#12
Re: Can't Parse textbox.
Originally Posted by Mugsy323
Only 70. SMH
The first result searching on " numeric textbox" appears to be the sample in question. If this is the one you meant, it took 2 seconds to find. You could have easily posted the link rather than make me search for it.
And it doesn't work.
Not sure if you're deliberately being thick, but JMcIlhinney's exact words were:
Originally Posted by jmcilhinney
I suggest that you follow the CodeBank link in my signature and check out my thread on creating a Numeric TextBox. You can use what I provided as is or you can just take the bits you want to use in your own code.
After clicking on his VB link, next to My CodeBank Submissions, I did Ctrl + F on my keyboard and typed in "numeric". Lo and behold, this came up: https://www.vbforums.com/showthread....meric-Text-Box
-
Jul 2nd, 2021, 10:36 AM
#13
Thread Starter
Lively Member
Re: Can't Parse textbox.
Originally Posted by dday9
Not sure if you're deliberately being thick, but JMcIlhinney's exact words were:
After clicking on his VB link, next to My CodeBank Submissions, I did Ctrl + F on my keyboard and typed in "numeric". Lo and behold, this came up: https://www.vbforums.com/showthread....meric-Text-Box
He has four links to separate code samples in his signature. He did not specify none of those were the links he was referring to (they aren't.) So after clicking one of the links, and figuring out on my own there were more examples one level up, I then searched for "numeric textbox", checked the first result, tested the code, and it didn't work.
So rather than test every example until I find one that worked, I asked for a direct link.
You may call that "thick". I call that a reasonable request when someone is being unnecessarily difficult. No matter. "DataMiser"s suggestion did the trick. SMH.
-
Jul 2nd, 2021, 12:28 PM
#14
Re: Can't Parse textbox.
Originally Posted by DataMiser
Why not use the keypress event and e.handled=true to suppress the keys you do not want?
Also you want to remember to allow the backspace key.
Originally Posted by Mugsy323
Nice. I didn't know that was an option. Everything I tried was going to the Textbox window first.
I did it with two lines of code:
Code:
If e.KeyChar.ToString < "0" Or e.KeyChar.ToString > "9" Then e.Handled = True
If e.KeyChar.ToString = vbBack Then e.Handled = False
Big thanks.
Originally Posted by Mugsy323
He has four links to separate code samples in his signature. He did not specify none of those were the links he was referring to (they aren't.) So after clicking one of the links, and figuring out on my own there were more examples one level up, I then searched for "numeric textbox", checked the first result, tested the code, and it didn't work.
So rather than test every example until I find one that worked, I asked for a direct link.
You may call that "thick". I call that a reasonable request when someone is being unnecessarily difficult. No matter. "DataMiser"s suggestion did the trick. SMH.
Now copy some text and paste it in...
-tg
-
Jul 2nd, 2021, 12:54 PM
#15
Re: Can't Parse textbox.
Now copy some text and paste it in...
I thought the same thing. But the KeyPress event is triggered and will catch it.
Edit:
Your right, if you use the copy/paste menu it will bypass the validation. I was using Ctl + V.
Last edited by wes4dbt; Jul 2nd, 2021 at 01:00 PM.
-
Jul 2nd, 2021, 01:02 PM
#16
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jul 2nd, 2021, 01:30 PM
#17
Re: Can't Parse textbox.
As a side note, here's what you may run into with those "two lines of code"...
The arrow keys don't work, the shift key doesn't work, can't select anything using the keyboard... the delete key no longer works, can't delete the contents, the tab key no longer works, can't tab out of the field...
Honestly You'd be better off accepting the input., what ever it is, then using validation after the user has left the field. Give them a hint on the way in and a smack on the head on the way out. Trying to smack their fingers as they type is not a lot of fun. And no matter how much you think you've got it buttoned up... they will find a way. Users are... a unique lot... they don't think like normal people. Some of the things I've had to deal with in the past the only way I can think of how they got there was a monkey randomly clicking away... and there I was, with a defect. "Why would you do that?" "Because I could!"
-tg
-
Jul 2nd, 2021, 01:59 PM
#18
Re: Can't Parse textbox.
Originally Posted by techgnome
As a side note, here's what you may run into with those "two lines of code"...
The arrow keys don't work, the shift key doesn't work, can't select anything using the keyboard... the delete key no longer works, can't delete the contents, the tab key no longer works, can't tab out of the field...
Don't you know, Mugsy323's users will never use any of those keys, never use the context menu to paste values, or any other edge case nor will Mugsy323 ever need to come back to this input once it has been rolled out.</s>
@Mugsy323 - If you think that I'm deliberately being a -insert any derogatory adjective here- it is because I am. You see, many users come here asking about this specific question and too often they will implement your exact solution, only to come back some time later because of some edge case. This specific question is not unique in any sense, chances are someone has had the exact use case you're facing. That is why people like JMcIlhinney and Delaney have proven (open source) solutions for you to literally copy/paste into your project.
Don't be too prideful to use their existing solutions. If you spurn their help, then chances are you'll be right back here asking about an edge case that has already been addressed.
-
Jul 2nd, 2021, 02:07 PM
#19
Re: Can't Parse textbox.
Originally Posted by Mugsy323
He has four links to separate code samples in his signature. He did not specify none of those were the links he was referring to (they aren't.) So after clicking one of the links, and figuring out on my own there were more examples one level up, I then searched for "numeric textbox", checked the first result, tested the code, and it didn't work.
So rather than test every example until I find one that worked, I asked for a direct link.
You may call that "thick". I call that a reasonable request when someone is being unnecessarily difficult. No matter. "DataMiser"s suggestion did the trick. SMH.
There is actually a 5th CodeBank link in his signature. It may not be super obvious, but the "VB" hyperlink right after "My CodeBank Submissions:" is the URL I believe he is referring to. It lets you search the CodeBank for his threads.
Good luck.
-
Jul 3rd, 2021, 12:59 AM
#20
Re: Can't Parse textbox.
Originally Posted by OptionBase1
There is actually a 5th CodeBank link in his signature. It may not be super obvious, but the "VB" hyperlink right after "My CodeBank Submissions:" is the URL I believe he is referring to. It lets you search the CodeBank for his threads.
Good luck.
When you have to explain how to use the internet and a web browser to someone who wants to create software for themselves, I'm not sure I'd be trusting that software.
Originally Posted by OptionBase1
It may not be super obvious
It's definitely obvious enough to someone prepared to make an effort.
-
Jul 3rd, 2021, 01:07 AM
#21
Re: Can't Parse textbox.
Originally Posted by Mugsy323
He has four links to separate code samples in his signature. He did not specify none of those were the links he was referring to (they aren't.)
I didn;t think I'd need to, given that I specifically stated that it was the thread about a Numeric TextBox and all the other titles indicate completely unrelated topics.
Originally Posted by Mugsy323
So after clicking one of the links, and figuring out on my own there were more examples one level up
Well done you, but everyone else has figured out that they just needed to click the VB link in my signature for VB CodeBank examples.
Originally Posted by Mugsy323
I then searched for "numeric textbox", checked the first result, tested the code, and it didn't work.
So rather than test every example until I find one that worked, I asked for a direct link.
You may call that "thick". I call that a reasonable request when someone is being unnecessarily difficult.
I don't know whether you're even referring to one of my threads or someone else's in the CodeBank but I've tested my code and it works so if you used it and it didn't work for you then it's probably something you did. Maybe you should have indicated what thread you actually used and what issue you had and then we could have worked out the problem. Regardless, I don't keep specific links at my fingertips so, to provide them to you, I would have to do the exact same thing as you would, i.e. click the link in my signature, look down or search the list to find the right one and then click it. I credited you with being able to do that for yourself. If I was wrong, what else should that be called other than thick? I created that thread to help people like you so I don't expect to have to hand it to you on a silver platter as well. If you can't/won't navigate a web page/site then I can't/won't do anything for you.
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
|