Results 1 to 12 of 12

Thread: could someone help me with finding a word in this textbox, please?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    could someone help me with finding a word in this textbox, please?

    Hi, I can't for the life of me figure out the code for this. I want to find the word 'hello'. If 'hello' appears more than once, I want it to find the next 'hello', and so on. I've looked at numerous snippits of code but all this seltext, selposition, sellength stuff has made me goggley eyed.

    I've attached my code below, in the hope someone can help. When the first 'hello' is found, it should move on to find the next one.

    Thanks for any help!

    Private Sub Command1_Click()
    Dim intStart As Integer
    Dim lenSelected, posSelected As Long
    intStart = InStr(1, Text1.Text, "hello")

    Text1.SetFocus
    Text1.SelStart = intStart - 1
    Text1.SelLength = Len("hello")

    If Len(Text1.SelText) <> 0 Then
    Text1.SetFocus
    posSelected = InStr(1, Text1, Text1.SelText)
    lenSelected = Len(Text1.SelText)
    Text1.SelStart = Len(Text1)
    Text1.SelStart = posSelected - 1
    Text1.SelLength = lenSelected
    End If
    End Sub

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Moon pie and chips...£67.99!!!

    U can only highlight one "Hello" at a time...do you want a message box to say "Find Next?" and then it finds the next hello in the text, and so on....???

  3. #3
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    Hi,

    Modified your code a bit...

    VB Code:
    1. Dim i As Long
    2. Dim sSearch As String
    3. sSearch = "hello"
    4. Text1.SelStart = i
    5. Text1.SelLength = 0
    6. For i = 1 To Len(Text1.Text)
    7.   If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
    8.     Text1.SelStart = i - 1
    9.     Text1.SelLength = Len(sSearch)
    10.     Text1.SelBold = True
    11.     Text1.SelColor = vbRed
    12.   End If
    13. Next

    You ll see that when you click th button, every 'hello' word is highlighted.
    If you want to make it a bit more flexible, place another textbox on your form, and place the value typed in in variabele sSearch.
    You can also place a button to make all text backt to normal after a search.

    Greetz, Luc

  4. #4
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    I think it could be quite easy. You just have to set the next starting point to the end of the hello. Which is in fact 5 position further. This way the text you look in shortens.

    eg.

    this is hello 1 and then hello 2.
    The first hello is at position 9 len =5. Your next start should thus be 14. the next hello is at 26 len=5. The thing is that the user will possibly never see the text being selected as a pc handles this too fast for the eye to see.

  5. #5
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    Like Killah said, you could search step by step too:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim i As Long
    4.  
    5. Private Sub Command1_Click()
    6.  
    7. Dim sSearch As String
    8. sSearch = "hello"
    9. Text1.SelStart = i
    10. For i = i To Len(Text1.Text)
    11.   If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
    12.     Text1.SelStart = i - 1
    13.     Text1.SelLength = Len(sSearch)
    14.     Text1.SelBold = True
    15.     Text1.SelColor = vbRed
    16.     i = i + 1
    17.     Exit For
    18.   End If
    19. Next
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23.   i = 1
    24. End Sub

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Note:

    .SelBold and .SelColor are not properties of a standard textbox (like Text1), but properties of a RichTextBox. Consequently, the word bolding and highlighting will only work if using the latter control.

  7. #7
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    Iguessed ianpaisley was using the richtextbox allready, since he got some code allready....

    So ianpaisley, if you are trying to solve this with a standard textbox, remove him from your form, and place a richtextbox instead. (Project / Components / Microsoft rich textbox control)

    Greetz, Luc

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    LucGuldentops & Co, thanks for your help. I was hoping I could use my code because I spent a lot of effort putting the damn thing together, and want to know how it works.

    Wokawidget, I don't want a 'Find Next' box - it will automatically find and highlight the next 'hello' when the command button is clicked.


    I thought replacing intStart with:
    intStart = InStr(intStart + Len("hello"), Text1.Text, "hello")
    would work, but it doesn't, unfortunately. Do I need a For....Next statement or something?

    Grateful if you could help, thanks again

  9. #9
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    ianpaisley,

    i believe this code (from above)will do the thing, not?

    VB Code:
    1. Dim i As Long
    2. Dim sSearch As String
    3. sSearch = "hello"
    4. Text1.SelStart = i
    5. Text1.SelLength = 0
    6. For i = 1 To Len(Text1.Text)
    7.   If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
    8.     Text1.SelStart = i - 1
    9.     Text1.SelLength = Len(sSearch)
    10.     Text1.SelBold = True
    11.     Text1.SelColor = vbRed
    12.   End If
    13. Next

    Greetz, Luc

  10. #10
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    ianpaisley,

    Never use code because you spent a long time working on it, if it isn't good, and you got something else, throw it away (or comment it out)

    Luc,

  11. #11
    Member JuiCe's Avatar
    Join Date
    May 2001
    Location
    Philly
    Posts
    46


    It seems to me that he wants to use his own code but only slightly modified to work. I could be wrong, but just in case, I modified his code to work how I think he wants it?


    Code:
    'Added this to hold the new position to search from
    Dim newPos As Long
    
    Private Sub Command1_Click()
        Dim intStart As Integer
        Dim posSelected, lenSelected As Long
    
            'Here you search from the new position
            intStart = InStr(newPos, Text1.Text, "hello")
    
        'If it couldn't find "hello" again, start from the beginning
        If intStart = 0 Then
            newPos = 1
            intStart = InStr(newPos, Text1.Text, "hello")
        End If
    
            Text1.SetFocus
            Text1.SelStart = intStart - 1
            Text1.SelLength = Len("hello")
    
        If Len(Text1.SelText) <> 0 Then
            Text1.SetFocus
            posSelected = InStr(newPos, Text1, Text1.SelText)
            lenSelected = Len(Text1.SelText)
            Text1.SelStart = Len(Text1)
            Text1.SelStart = posSelected - 1
            Text1.SelLength = lenSelected
    
            'Set the new position to just after the last found word
            newPos = Text1.SelStart + Len("hello")
        End If
    End Sub
    
    Private Sub Form_Load()
        'Make sure the start position is 1 at first
        newPos = 1
    End Sub


    Now I know I could've made this code much more efficient and cleaner, but it seems to me he just wants to understand how the code works and what better way to learn than from his own code? If he didn't want his own code modified - oops


  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    JuiCe, your code did EXACTLY what I wanted to acheive - positioning 'hello' on first line and all. Thanks again to everyone.

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