Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Word Searching in a text box

How can I make a procedure that will search through a block of text in a text box (or other control) and find a specified word or words within it.
0
Help_me
Asked:
Help_me
1 Solution
 
deightonCommented:
Use the Instr command
0
 
deightonCommented:
Here is an example

Private Sub Command1_Click()

    Dim c As Long
   
    c = InStr(Text1.Text, "hello")
   
    If c > 0 Then
   
        MsgBox "hello" & " found at position " & CStr(c)
       
    End If


End Sub


0
 
Help_meAuthor Commented:
I've boosted the points because this one's important. Following on from the above procedure, I need to expand this utility so that it:

     searches through the text and finds the word (as above), and highlights it. If the word is out of view, the program should scroll down to it.

     I would also like to be able to do a word count.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
deightonCommented:
Looks like you'll need to use the rich text box.  I can work on it
0
 
ikxsoftCommented:
to highlight the word use the sellength, selstart properties of the text box

selstart specifies where to start highlighting, the instr function will give you this
sellength is how many characters you want to highlight
0
 
Help_meAuthor Commented:
I can do this using the rich text box, although it will not actually select the text, it still appears unselected despite the SelStart and SelLength properties containing the correct values. I also need to know how to load text into the box using the common dialog box.
0
 
Help_meAuthor Commented:
Que?
0
 
bear454Commented:
Assuming a richtextbox named RichTextBox1, and a common dialog name CommonDialog1, and a command button named Command1, the file could be loaded as follows:

Private Sub Command1_Click()
    CommonDialog1.ShowOpen
    On Error GoTo ErrHandle
    RichTextBox1.LoadFile CommonDialog1.filename
    Exit Sub
ErrHandle:
End Sub

Given a textbox containing the desired search string named Text1, and a command button for searching named Command2, the following will select and highlight the substring, progressing through the document.
(Assumes RichTextBox1.HideSelection = False)

Private Sub Command2_Click()
    If RichTextBox1.Find(Text1.Text, _
      IIf(RichTextBox1.SelLength = 0, 0, _
      RichTextBox1.SelStart + RichTextBox1.SelLength), _
      Len(RichTextBox1.Text) - 1) = -1 Then
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = 0
        MsgBox "Finished searching document."
    End If
End Sub

Any other questions? =}

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now