Results 1 to 15 of 15

Thread: Check text box for characters.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    27

    Question Check text box for characters.

    I want to check if text box has “?” character or specific numbers (ex "14"). How can I do that? It may have all kind of other letters too.

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: Check text box for characters.

    Code:
    textbox1.text.indexof("What you want to find")
    Will return an integer of the position of the specified value. As far as I remeber will return -1 is it doesnt find the specified value in the string.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    27

    Re: Check text box for characters.

    Thanks zed

    Code:
            If (TextBox1.Text.IndexOf("?") > -1) Then
                MsgBox("?")
            End If
    this works great but are here any other methods?

  4. #4
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Check text box for characters.

    read up on regular expressions.
    personally, I suck at them but there are some forum members that can whip 'em out like nothing.

    I'm sure they'll be here shortly to offer some neat ways to search for certain strings.

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check text box for characters.

    Here:
    vb.net Code:
    1. If Me.TextBox1.Contains("?") Then
    2.        'There's a ? mark
    3. End If
    4. Dim numberstofind() As Integer = {50,25,193,1847,2,30}
    5. Dim n As Integer
    6. For Each n In numberstofind
    7.       If Me.TextBox1.Contains(n.ToString()) Then
    8.              'There's the number n.
    9.       End If
    10. Next

  6. #6

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check text box for characters.

    Yes. But where's the problem?

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Check text box for characters.

    well.. he might want people with grades of 10 or less on his math test.. he'll get all the kids that got 100's too. see the problem?

  9. #9
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Check text box for characters.

    Then the grades would be separated by some character and you just have to append that character to the end of number in string form.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  10. #10
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Check text box for characters.

    it will still find 10 and 100 and it may not be as simple as character-delimited.

    "I recieved a 10 on the test. 100 was the highest grade."

    it would find both numbers.

    OP could be more specific on the string he's looking or we are just left to assume

  11. #11
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Check text box for characters.

    No, you see they are separated, by a space. Just add a space " " on the back of the number. Now yes it can get complicated with periods and commas but just add them to a string of characters (call them ending characters or something) and loop through it checking each time.

    or you could even right a function to make sure that after the number is not another digit, like:

    Code:
    Dim LetterAfter as String = Val(Text.Substring(Index + Number.Length, 1)
    If Val(LetterAfter) = 0 and LetterAfter <> "0" and LetterAfter <> "." Then
    'It is the exact number
    End If
    I just came up with that so it may have to be modified but it's not a hard task.
    Last edited by Vectris; Jul 27th, 2009 at 11:07 PM.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  12. #12
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Check text box for characters.

    jsut sayin an array of numbers/strings isn't the best idea. a regular expression would be better suited.

    in that case it would find 10, and 100 then put a space at the end of each one.
    you'd have 10[space] and 100[space], you'd have to split that string to get an exact match. a lot of work would be involved to do all that. a regex would handle that much better.

  13. #13
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Check text box for characters.

    Well it's not a bad idea, how wouldn't it work?

    Also made an edit and added another idea.

    You are right in that RegEx would probably work better, but most people, including me, don't know how to use it.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  14. #14
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check text box for characters.

    RegEx is actually very easy to learn, and I would recommend it, but I think that in this case it would probably be slower. The objective is to see if the characters are in the TextBox. If you need to find numbers in that way, you can use RegEx, but in this case, making the regular expression would be much harder than usual. I'll wait and see what AlexWu says.

  15. #15
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check text box for characters.

    Actually, come to think of it, this would be better:
    vb.net Code:
    1. If Me.TextBox1.Text.Contains("?") Then
    2.        'There's a ? mark
    3. End If
    4. Dim numberstofind() As Integer = {50,25,193,1847,2,30}
    5. For Each n As Integer In numberstofind
    6.       If Me.TextBox1.Text.Contains(" " & n.ToString() & " ") Then
    7.              'There's the number n.
    8.       End If
    9. Next

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