AllExperts > Visual Basic 
Search      
Visual Basic
Volunteer
Answers to thousands of questions
 Home · More Visual Basic Questions · Answer Library  · Encyclopedia ·
More Visual Basic Answers
Question Library

Ask a question about Visual Basic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Golfnut_1969
Expertise

Instantiating form variables instead of using Global instances.
User Defined Types
DLLs, ActiveX Exes
String Manipulation
Working with Classes
Custom User Controls
Object Oriented Programming
Database access (SQL Server, MS Access, RDO, DAO, ADO)
Use of Arrays

I do not answer questions regarding databound controls so please ask those questions of one of the other experts.

Experience
I've been working with Visual Basic 5.0 and 6.0 for over 8 years. I've worked extensively with multi-tier database business solutions created in Visual Basic. I've also been working in Visual Basic .NET for over 2 years and Visual Basic .NET 2.0 since its first BETA release. I currently work developing custom applications for a variety of industries. In addition to all the work in Visual Basic 6.0 and .NET I have also worked extensively with ASP .NET and ASP .NET 2.0.

Education/Credentials
Associate’s Degree: Computer Programming St. Louis Community College at Florissant Valley (4.0 gpa) Microsoft Certified Technology Specialist (MCTS) .NET Framework 2.0 Windows Applications

Past/Present clients
Mallinckrodt Pharmaceuticals Commerce Bank CitiMortgage Elmwood Marine Services FedEx Memco Barge Lines Crounse Corporation AG Edwards

 
   

You are here:  Experts > Computing/Technology > Basic > Visual Basic > Creating a find word in document

Visual Basic - Creating a find word in document


Expert: Golfnut_1969 - 5/8/2007

Question
i want to create a program which can find a specified word in a
rich text box of a visual basic form and can highlight the word and move the cursor to that word. plz help me with the coding  

Answer
I've create a sample you can use to figure this one out. Basically you want to use the Instr function to find the text. You can use the SelStart and SelLength properties of the textbox to highlight the word and move the cursor once found.

I've created a new form and added a richtextbox named txtDocument. I've added a regular textbox named txtFind. Finally I've added a button named cmdFind.  Here is the code to search for text. Each time you click the button it searches for the next match.

Option Explicit

Private Sub cmdFind_Click()
   ExecuteSearch
End Sub

Private Sub ExecuteSearch()
   Dim sFind As String
   Dim nFindIndex As Integer
   Dim nStartIndex As Integer
   
   sFind = txtFind.Text
   
   nStartIndex = txtDocument.SelStart + txtDocument.SelLength
   
   If nStartIndex = 0 Then
       nStartIndex = 1
   ElseIf nStartIndex >= Len(txtDocument.Text) Then
       If PromptForSearchFromBeginning Then
           txtDocument.SelStart = 1
           txtDocument.SelLength = 0
           nStartIndex = 1
       End If
   End If
   
   nFindIndex = InStr(nStartIndex, txtDocument.Text, txtFind.Text, vbBinaryCompare)
   
   If nFindIndex > 0 Then
       txtDocument.SelStart = nFindIndex - 1
       txtDocument.SelLength = Len(txtFind.Text)
       txtDocument.SetFocus
   Else
       If InStr(1, txtDocument.Text, txtFind.Text, vbBinaryCompare) Then
           If PromptForSearchFromBeginning Then
               txtDocument.SelStart = 1
               txtDocument.SelLength = 0
               'Make recursive call
               ExecuteSearch
           End If
       End If
   End If
End Sub

Private Function PromptForSearchFromBeginning() As Boolean
   Dim eMsgBxRslt As VbMsgBoxResult
   Dim bRetVal As Boolean
   
   eMsgBxRslt = MsgBox("End of Document reached" & vbNewLine & "Would you like to start from the beginning?", vbYesNo, "End of Document")
   bRetVal = (eMsgBxRslt = vbYes)
       
   PromptForSearchFromBeginning = bRetVal
End Function  

View Follow-Ups    Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.