Results 1 to 21 of 21

Thread: Can't Parse textbox.

  1. #1

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Question 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.

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

    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.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,181

    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?

  4. #4

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Can't Parse textbox.

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

  5. #5

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Can't Parse textbox.

    Quote Originally Posted by jmcilhinney View Post
    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?

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

    Re: Can't Parse textbox.

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

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

    Re: Can't Parse textbox.

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

  8. #8

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Can't Parse textbox.

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

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

    Re: Can't Parse textbox.

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

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  11. #11

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Can't Parse textbox.

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

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Can't Parse textbox.

    Quote Originally Posted by Mugsy323 View Post
    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:

    Quote Originally Posted by jmcilhinney View Post
    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Can't Parse textbox.

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

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Can't Parse textbox.

    Quote Originally Posted by DataMiser View Post
    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.
    Quote Originally Posted by Mugsy323 View Post
    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.
    Quote Originally Posted by Mugsy323 View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,181

    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.

  16. #16
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Can't Parse textbox.

    you can use mine (all or some part of the code)

    https://www.vbforums.com/showthread....ion&highlight=
    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)

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Can't Parse textbox.

    Quote Originally Posted by techgnome View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  19. #19
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Can't Parse textbox.

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

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

    Re: Can't Parse textbox.

    Quote Originally Posted by OptionBase1 View Post
    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.
    Quote Originally Posted by OptionBase1 View Post
    It may not be super obvious
    It's definitely obvious enough to someone prepared to make an effort.

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

    Re: Can't Parse textbox.

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



Click Here to Expand Forum to Full Width