Link to home
Create AccountLog in
Avatar of bertino12
bertino12

asked on

Bold the correct substring in a richtextbox

I have a richTextBox control and it contains text. I am putting in the ability to make a highlight some text and make it bold. The problem I am having is that if I want to highlight a word or phrase that appears multiple times it is highlighting the first instance.

For example:
I went to the store.

I went to the airport

I went to the movies.

I highlight [I went] on the third line. It will just highlight the first occurence of "I went". I want it to only highlight the 3rd occurence. How can I made this so it works properly?
ASKER CERTIFIED SOLUTION
Avatar of sagacitysolutions
sagacitysolutions
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
To extend this functionality you could also check to see if the selection was already bolded and reverse the bold.

If rtb.SelectionFont.Bold Then
    rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular)
Else
    rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold)
End If

Avatar of bertino12
bertino12

ASKER

Yeah, but I have a Bold Button that is going to make the selection bold. Its just not able to highlight the correct instance of the substring. So you have to click a button to Bold the highlighted portion of the text.
I dont quite understand your question then.
You said you already highlighted the third occurance but something is causing it to select the first occurance, what event is causing the first instance of "I went" to be selected.

Do you want all instances of "I went" to be highlighted or are you trying to re-Highlight the the third instance that you said was already highlighted?

Can you please clarify your question