Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for text 2

Status
Not open for further replies.

pmrankine

Programmer
Jul 18, 2001
71
GB
Hi,
I have used the following code (given by GTO in thread thread222-109634) to highlight searched for text - it works fine. I'd like to be able to highlight (make bold?) all instances of the text found and also more than one string at a time.

**CODE:
Option Explicit
Dim StartPosition


Private Sub cmdFindNext_Click()


StartPosition = InStr(StartPosition + 1, txtSearchIn, txtSearchFor)
If StartPosition <= 0 Then Exit Sub
txtSearchIn.SelStart = StartPosition - 1
txtSearchIn.SelLength = Len(txtSearchFor)
txtSearchIn.SetFocus
End Sub

Private Sub Form_Load()

StartPosition = 0
End Sub

**END OF CODE

We have a tool which allows users to search a text field in a database for particular words or phrases (not neccessarily adjacent to each other) and would like to highlight the words that they searched for in the results text box.

Any clues?
TIA
pmrankine
 
Actually, you might look at using a Microsoft Rich Text Box control if it suits your purposes otherwise. It already has a built in find command which can find multiple instances of a word. It's included with Visual Basic already, just add it to your Project from the Components menu.

J. Broadus
corsair@hiscommunications.com
 
HISCorsair,
I'm not too clued up on Rich Text Boxes - adding the component and adding the obect to the form is as far as I've gone. I think I can follow the code for the Find Method - I take it that this is what I should use.
To make the found word(s) bold, the selFontName or similar would do the trick??

Thanks for your help,
pmrankine
 
Ok, the following is illustrative rather than a comprehensive solution. In other words it shows a general idea rather than providing a fully worked solution. It requires a form with two rich text boxes on it (HISCorsair is basically right that a RichTextBox control is the easiest way of dealing with this), and a Command button. Then just drop this code into the form:
[tt]
Option Explicit

Private Sub Command1_Click()
RichTextBox1.Text = &quot;Now is the winter of our discontent made glorious by - ar actually, here are some more words just for the hell of it: the winter of discontent once more now&quot;
RichTextBox2.Text = &quot;&quot;
EnboldStrings &quot;now, winter&quot;, RichTextBox1
End Sub


Public Sub EnboldStrings(strStrings As String, RTF As RichTextBox)
Dim strEnbolden() As String
Dim lp As Long

strEnbolden = Split(strStrings, &quot;,&quot;)
For lp = LBound(strEnbolden) To UBound(strEnbolden)
EnboldString strEnbolden(lp), RTF
Next
End Sub
Public Function EnboldString(strText As String, RTF As RichTextBox)
Dim re As RegExp
Dim strReplace() As String

Set re = New RegExp
re.Global = True
re.IgnoreCase = True

' All of this jusat to set the highlighting type...
RichTextBox2.Text = strText
RichTextBox2.SelStart = 0
RichTextBox2.SelLength = Len(strText)
RichTextBox2.SelBold = True ' OK - we're just going to use bold

strReplace = Split(RichTextBox2.SelRTF, strText)
re.Pattern = &quot;(&quot; + strText + &quot;)&quot;
RTF.TextRTF = re.Replace(RTF.TextRTF, strReplace(0) + &quot;$1&quot; + strReplace(1))

End Function
 
strongm and HISCorsair,

thank you both for your help - I've now got something that will work.

Cheers,
pmrankine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top