By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

Search in a rich textbox

P: 32
could somebody please tell me how to search a rich textbox for a particular word and then highlight that word in the rich textbox
;
;
on the form theres are textbox called txtsearch, a button called btnsearch and a rich textbox called rtbtext. The person enters a word into the textbox, presses the button and then it highligts that word in the richtextbox like the crtl F function in word and notebook etc.
Apr 9 '08 #1
Share this Question
Share on Google+
5 Replies


100+
P: 133
could somebody please tell me how to search a rich textbox for a particular word and then highlight that word in the rich textbox
;
;
on the form theres are textbox called txtsearch, a button called btnsearch and a rich textbox called rtbtext. The person enters a word into the textbox, presses the button and then it highligts that word in the richtextbox like the crtl F function in word and notebook etc.

Hi Barry M,
i dont know what version VB you are using but this is for
VB 6

Code: ( vb 6 )
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim Search, Where
  3.     ' Get search string from user.
  4.         Search = Text1.Text 'this is the text to search
  5.     ' Find string in text.
  6.     Where = InStr(RichTextBox1.Text, Search)
  7.     If Where Then
  8.         RichTextBox1.SetFocus
  9.         RichTextBox1.SelStart = Where - 1
  10.         RichTextBox1.SelLength = Len(Search)
  11.     Else
  12.         MsgBox "String not found."
  13.     End If
  14. End Sub
GobbleGob.
Apr 9 '08 #2

P: 32
anybody got the coding for vb 2005
Apr 9 '08 #3

100+
P: 133
anybody got the coding for vb 2005
this works in vb2008

Expand|Select|Wrap|Line Numbers
  1.     'Make sure you declare this
  2.     Private intPosition As Integer
  3.  
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Static intStart As Integer
  7.  
  8.         'used to select compare method
  9.         Dim objType As Object
  10.         objType = CompareMethod.Text
  11.  
  12.         'set starting position to 1
  13.         intPosition = 1
  14.         'use the InStr function to look up a staring position of a search string in a given text box using objType (case-insensitive or case-sensitive)
  15.         intStart = InStr(intPosition, Me.txtText.Text, Me.txtSearch.Text, objType)
  16.  
  17.         If intStart > 0 Then
  18.             'set starting select position on a textbox and select the search string
  19.             Me.txtText.SelectionStart = intStart - 1
  20.             Me.txtText.SelectionLength = Me.txtSearch.Text.Length 'highlights the searched word
  21.             Me.txtText.Select()
  22.         End If
  23.     End Sub
GobbleGob.
Apr 9 '08 #4

P: 32
thanx that works but do you know how to make if it that the person clicks it again it goes to the next matching word in the textbox
Apr 9 '08 #5

QVeen72
Expert 100+
P: 1,445
Hi,

for that, You will need to Check if any Word is already selected in the RichTextbox .. (SelLength Property), If already selected, then start Search from (SelStart+SelLength) position instead of First position...

Regards
Veena
Apr 9 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.