Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word Searching in a text box

Posted on 1999-01-13
8
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1455971
Use the Instr command
0
 
LVL 18

Expert Comment

by:deighton
ID: 1455972
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
ID: 1455973
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:deighton
ID: 1455974
Looks like you'll need to use the rich text box.  I can work on it
0
 
LVL 1

Expert Comment

by:ikxsoft
ID: 1455975
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
ID: 1455976
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
ID: 1455977
Que?
0
 
LVL 1

Accepted Solution

by:
bear454 earned 300 total points
ID: 1455978
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

705 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