Solved

Word Searching in a text box

Posted on 1999-01-13
8
179 Views
Last Modified: 2010-05-03
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
Comment
Question by:Help_me
8 Comments
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Use the Instr command
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
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
 

Author Comment

by:Help_me
Comment Utility
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
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Looks like you'll need to use the rich text box.  I can work on it
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:ikxsoft
Comment Utility
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
 

Author Comment

by:Help_me
Comment Utility
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
 

Author Comment

by:Help_me
Comment Utility
Que?
0
 
LVL 1

Accepted Solution

by:
bear454 earned 150 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now