Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

In a RichTextBox when I highlight a string the previously highlighted string loses its highlighting.

$
0
0

I hope someone here can help me, I've been struggling with this for many hours.

I have two RichTextBoxes.  In both cases I need to highlight text which is already in the RTB.  (So I can't use Append.)

In both cases I use, basically, SelectionStart, SelectionLength, and SelectionColor in a loop.  In one RTB that works fine. In the other, as soon as I highlight another string the previously highlighted string loses its highlighting!  If it might matter this is happening on Windows 7, .Net 4.5, x64. 

Here's the code ...

With rtbOriginal
     '.Focus()
      .SelectionStart = HitLoc
     '.ScrollToCaret()
     'The SelLength property is set to 0 as
     'soon as you change SelStart
      .SelectionLength = ci.OldStr.Length
      '.Select(HitLoc, ci.OldStr.Length)
      .SelectionColor = Color.Red
End With
MsgBox("Highlighted """ & ci.OldStr & """")

In the case that works the Focus and ScrollToCaret are not commented out, but here I can comment them out or not and that doesn't make any difference. I've also tried using just Select instead of SelectionStart/SelectionLength and that made no difference. With the MsgBox statement I can see that when the message box pops up the previously highlighted word is no longer highlighted, only the most recently one is.  Using the debugger I've gone through the code statement by statement, and again each trip through the loop the previoulsy highlighted string loses its highlighting.

Both of the RTBs (the one that the code works for and the one that it doesn't work for) are in a UserControl and there are several instances of the UserControl on my Form. Consistently the code does not work for the "rtbOriginal" RTB but does for the other one.

If anyone has any ideas I'd sure like to hear them. I can't use an Append technique because the text is already in the RTB, but I'd welcome any other techniques.

Thanks,  Bob


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>