|
-
Jan 29th, 2002, 12:42 AM
#1
Thread Starter
Junior Member
Searching a Text Box
I've got a textbox I'd like to search for a particular word.
Let's say that I've got a textbox called txtTest and it's text property is equal to "Hello and thank you for helping me" I'd like to search this textbox for the word "you"
If it contains this word then show me a messagebox telling me so.
If it doesn't contain this word, show me a messagebox telling me it's not there.
How do I do this?
Thanks!
Reid
-
Jan 29th, 2002, 03:04 AM
#2
Junior Member
Ok, let's say you have a TextBox on your form Called txtInfo. Here is a little code snippet that will do what you want.
Dim strPos as Integer
' The following line will search for 'you' in the textbox and return
' the position in the textbox
strPos = Instr(txtInfo.Text, "you")
' If the position returned is greater than 0 then we have
' found 'you' so show its position.
If strPos>0 Then MsgBox("I Found 'you' at the position: " & strPos.ToString)
I hope this helps.
[COLOR=skyblue]
-
Jan 29th, 2002, 11:23 AM
#3
Thread Starter
Junior Member
Thanks! That works well! Now can we take it a step further and ask this?
Once we've indentified the word we were looking for, can we replace it? In your code above, we were looking for the word "you". Let's say once we've found "you" we want to replace it with "me".
Is this possible?
Thanks!
Reid
-
Jan 29th, 2002, 11:43 AM
#4
Hyperactive Member
VB Code:
txtTest.Text.Replace("You", "Me")
-
Jan 29th, 2002, 02:58 PM
#5
Junior Member
Well Bananafish supplied exactly what you need.
-
Jan 29th, 2002, 05:37 PM
#6
Thread Starter
Junior Member
I'm having trouble adapting this code for a multiline textbox. The textbox I'd like to search contains carriage returns and about 30 lines.
I know, thanks to Ozki1976 the position of any word in my text box. How do I take Bananafish's code and implement it using Ozki1976's code? Do I even need to use both? I'd like to replace the one word in a group of say 1000. I can't find a way to use banafish's code if there's more than one word in the textbox.
Thanks!
Reid V. Plumbo
-
Jan 30th, 2002, 05:01 AM
#7
Junior Member
OK, I will use the previous code with a few mods/additions to allow for you to change the 'you' to 'me'.
Dim strPos as Integer
' The following line will search for 'you' in the textbox and return
' the position in the textbox
strPos = Instr(txtInfo.Text, "you")
' If the position returned is greater than 0 then we have
' found 'you' so now replace it with 'me'.
If strPos > 0 Then
' The Mid function returns a string from within another string
' based on the start position of 1 and the length of strPos.
' strPos being the start of the word 'you'.
' If you don't specify a length then it is considered to be the
' length of the remaining text.
txtInfo.Text = Mid(txtInfo.Text, 1, strPos - 1) & "me" & _
Mid(txtInfo.Text, strPos+3)
End If
I hope this helps.
-
Jan 30th, 2002, 05:06 AM
#8
Hyperactive Member
do you wish to replace all occurences of the word in the text box?
if you do, then the Replace method is what you can use,
For example.
VB Code:
textbox1.text = textbox1.Text.Replace("you", "me")
Will replace all occurances of the word "you" for the word "me", regardless of how many lines, crlf etc that exist in the text.
or do you just wish to replace a specific occurence - but leave the remaining ones? If you wish to do this then you would need to know the start position of the string to be replaced.
For example,
VB Code:
strText = "you"
intStart = InStr(TextBox1.Text, strText)
If intStart > 0 Then
If intStart = 1 Then
TextBox1.Text = "me" & TextBox1.Text.Substring(intStart + strText.Length)
Else
TextBox1.Text = TextBox1.Text.Substring(0, intStart - 1) & _
"me" & TextBox1.Text.Substring(intStart + strText.Length - 1)
End If
End If
-
Jan 30th, 2002, 05:07 AM
#9
Hyperactive Member
sorry ozki - I didn't see your reply.
-
Jan 30th, 2002, 05:11 AM
#10
Junior Member
No worries Bananafish, at least you considered if the word you was at the start of text... I didn't... oops
-
Jan 30th, 2002, 05:17 AM
#11
Hyperactive Member
I did, but I think I forgot a - 1. It should be
VB Code:
If intStart = 1 Then
TextBox1.Text = "me" & TextBox1.Text.Substring(intStart + strText.Length - 1)
Else
and what if the word is the last word? oh well.
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
|