Solved

Word Searching in a text box

Posted on 1999-01-13
8
181 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 150 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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