Hello guys!

I am totally new in visual basic and have got desperately stuck for 4 hours with this problem which I bet is very stupidly simple.

The simple program loads a .rtf file in a rich text box and using a textbox and a button, I have to search the worlds in that file.

The Find button uses this code.

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        rtfWindChill.Find(txtFind.Text, RichTextBoxFinds.MatchCase)
    End Sub

So this basically finds the first instance of the searched word and highlight but I have to find all the occurrence and highlight it. I guess there would be some sort of loop here but after going around google for hours, I am hopeless and have come to my last refugee. So any ideas would be hugely appreciated. Thank you.
6pandn21

Recommended Answers

All 6 Replies

Yes, you do need a loop

Dim FoundAtPosition As Integer

FoundAtPosition = RichTextBox1.Find("foo", RichTextBoxFinds.MatchCase)
Do Until FoundAtPosition < 0
  ' Highlight text
  ' Search next occurrence
  FoundAtPosition = RichTextBox1.Find("foo", FoundAtPosition + 1, RichTextBoxFinds.MatchCase)
Loop

Thank you, I will have a look on it and try to work it in my code.

6pandn21

Hello again guys!

Sorry for being sort of too much of a newbie here. But I am again stuck. I used the code that Teme64 provided into my program with very little modification which being changing the 'foo' to the input of my text box txtFind and changing the textbox1 to rtfWindChill.

When I run it, it does the same old thing: Search one occurrence and that's it. So the loop is not running but strangely when I search for single letters, it always go to the last occurrence of the letter and shows the program being refresh as if the loop is running. Being hopelessly a beginner, I can sense what's happening. I hope I am not complaining too much.

The code is below

Dim FoundAtPosition As Integer

        FoundAtPosition = rtfWindChill.Find(txtFind.Text, RichTextBoxFinds.MatchCase)

        Do Until FoundAtPosition < 0

            ' Highlight text

            ' Search next occurrence

            FoundAtPosition = rtfWindChill.Find(txtFind.Text, FoundAtPosition + 1, RichTextBoxFinds.MatchCase)
        Loop

Thank you.
6pandn21

Here's a "debug" version. Replace RichTextBox1 and "foo" like you did before

Dim FoundAtPosition As Integer

FoundAtPosition = RichTextBox1.Find("foo", RichTextBoxFinds.MatchCase)
If FoundAtPosition < 0 Then
  MessageBox.Show("Search string '" & "foo" & "' does not exist", "Find", _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Do Until FoundAtPosition < 0
  ' Highlight text
  MessageBox.Show("Search string '" & "foo" & "' found at the position: " & FoundAtPosition.ToString, "Find", _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
  ' Search next occurrence
  FoundAtPosition = RichTextBox1.Find("foo", FoundAtPosition + 1, RichTextBoxFinds.MatchCase)
Loop

Now you should either get message "Search string '<string to find>' does not exist" or message(s) "Search string '<string to find>' found at the position: x" where x is the position of the occurrense(s). Positions start from 0, and -1 returned by Find method indicates that the string does not occur in the text.

To put it in the other way, the code above shows you a messagebox for each occurrence of the string you're searching. If you search, for example, a single character and it does exist multiple times, the loop should show the messagebox as many times.

A few points. Your search is case sensitive, so 'a' does not match with 'A'. Also could it be possible that your txtFind.Text contains some extra characters (white spaces)? You could try txtFind.Text.Trim to strip leading and trailing white spaces away.

Thank you Teme64 for the reply. I have managed to make it work although it does not completely do what I want it to. Like every time it goes to a new occurrence, I want the first one to be still highlighted so that when I add style to all the selected text, I won't have to do it one by one. I did some improvisation by changing the back colour of every selected text. And is there any easy way to change the text of the message box to something except OK and cancel? But still it does most of what I want. Would not have been possible without your help. Thank you.
6pandn21

AFAIK there's no other way to highlight occurrences except to do it one by one in a loop.

If you mean the message boxes in my sample code, you'll probably strip them off. They were only for "debugging" purpose. There are a few other options than OK or Cancel, like YesNo and AbortRetryIgnore. You should see them while you type the code. If you want some other options, you have to make your own MessageBox (control) class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.