Results 1 to 11 of 11

Thread: Searching a Text Box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    30

    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

  2. #2
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30

    Thumbs up

    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]
    <! Ozki !>

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    30
    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

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    VB Code:
    1. txtTest.Text.Replace("You", "Me")

  5. #5
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Well Bananafish supplied exactly what you need.
    <! Ozki !>

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    30
    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

  7. #7
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    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.
    <! Ozki !>

  8. #8
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    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:
    1. 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:
    1. strText = "you"
    2.       intStart = InStr(TextBox1.Text, strText)
    3.       If intStart > 0 Then
    4.          If intStart = 1 Then
    5.             TextBox1.Text = "me" & TextBox1.Text.Substring(intStart + strText.Length)
    6.          Else
    7.             TextBox1.Text = TextBox1.Text.Substring(0, intStart - 1) & _
    8.                             "me" & TextBox1.Text.Substring(intStart + strText.Length - 1)
    9.          End If
    10.  
    11.       End If

  9. #9
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    sorry ozki - I didn't see your reply.

  10. #10
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    No worries Bananafish, at least you considered if the word you was at the start of text... I didn't... oops
    <! Ozki !>

  11. #11
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    I did, but I think I forgot a - 1. It should be

    VB Code:
    1. If intStart = 1 Then
    2.             TextBox1.Text = "me" & TextBox1.Text.Substring(intStart + strText.Length - 1)
    3.          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
  •  



Click Here to Expand Forum to Full Width