I wish to be able to Highlight (select) text in a RichTextBox without affecting the current .SelectionBackColor or .SelectionColor
I have a RichTextBox containing a lot of text.
I have a ListView containing a list of strings/words which have different .BackColor and .ForeColor in the ListView.
I loop through the ListView items and highlight corresponding text in the RichTextBox using .SelectionBackColor and .SelectionColor setting the text color to match the .BackColor and .ForeColor from the ListView.
For Each verItem As ListViewItem In lvVer.Items
startindex = 0
While startindex < rtbMain.TextLength
Dim wordStartIndex As Integer = rtbMain.Find(verItem.Text, startindex, RichTextBoxFinds.None)
If wordStartIndex <> -1 Then
rtbMain.SelectionStart = wordStartIndex
rtbMain.SelectionLength = verItem.Text.Length
rtbMain.SelectionBackColor = verItem.BackColor
rtbMain.SelectionColor = verItem.ForeColor
Else
Exit While
End If
startindex += wordStartIndex + verItem.Text.Length
End While
Next
This all works perfectly, but I want to be able to select an item in the listview and then highlight the matching text object in the RichTextBox with the standard highlight (blue background, white text) colors whilst still retaining my original colors underneath.
If I use the mouse to highlight text in the RichTextBox, it gets highlighted temporarily in the usual windows fashion (blue background, white text). If I then click the mouse somewhere else in the RichTextBox, the original color I set on the string is still there. I want to replicate this behaviour in code.
If 'select' the text via code rtbMain.Select(wordStartIndex, verItem.Text.Length), it does not get highlighted. It doesn't even appear to be selected visually either. Obviously I can set a new .SelectionBackColor and .SelectionColor but then I will lose my original colors.
Is there a way to scroll the mouse over the RichTextBox text programatically and have the text selected and highlighted without affecting the underlying original colors?