-
Jul 27th, 2009, 05:12 PM
#1
Thread Starter
Junior Member
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.
-
Jul 27th, 2009, 05:46 PM
#2
New Member
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.
-
Jul 27th, 2009, 06:24 PM
#3
Thread Starter
Junior Member
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?
-
Jul 27th, 2009, 07:08 PM
#4
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.
-
Jul 27th, 2009, 07:20 PM
#5
Re: Check text box for characters.
Here:
vb.net Code:
If Me.TextBox1.Contains("?") Then 'There's a ? mark End If Dim numberstofind() As Integer = {50,25,193,1847,2,30} Dim n As Integer For Each n In numberstofind If Me.TextBox1.Contains(n.ToString()) Then 'There's the number n. End If Next
-
Jul 27th, 2009, 07:25 PM
#6
Re: Check text box for characters.
won't that find 500, 5000, 500000 as well? etc
-
Jul 27th, 2009, 08:11 PM
#7
Re: Check text box for characters.
Yes. But where's the problem?
-
Jul 27th, 2009, 08:47 PM
#8
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?
-
Jul 27th, 2009, 10:27 PM
#9
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.
-
Jul 27th, 2009, 10:58 PM
#10
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
-
Jul 27th, 2009, 11:04 PM
#11
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.
-
Jul 27th, 2009, 11:06 PM
#12
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.
-
Jul 27th, 2009, 11:09 PM
#13
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.
-
Jul 31st, 2009, 06:18 PM
#14
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.
-
Mar 28th, 2010, 07:19 PM
#15
Re: Check text box for characters.
Actually, come to think of it, this would be better:
vb.net Code:
If Me.TextBox1.Text.Contains("?") Then 'There's a ? mark End If Dim numberstofind() As Integer = {50,25,193,1847,2,30} For Each n As Integer In numberstofind If Me.TextBox1.Text.Contains(" " & n.ToString() & " ") Then 'There's the number n. End If 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|