0
votes

C# language, visual studio 2010 express

Is it possible to deselect text from a richtextBox without losing the actual highlighted text?

If I highlight another selected text I lose the old one, if I select the end of the text I lose everything I have highlighted.

Thanks a lot!!!

Here is the partial code:

(I call this code if a test is passed and I want to highlight the string that makes my test passed)

            int index = tb_log.Text.IndexOf(s.stringParse);

            tb_log.Select(index, s.stringParse.Length);
            tb_log.SelectionBackColor = Color.Lime;
            tb_log.SelectionColor = Color.Black;
            tb_log.SelectionFont = new Font(tb_log.Font, FontStyle.Bold);

Then to keep the serial's data readable by the user, i use this function to scroll the text of richtextbox till the end of the text:

            tb_log.SelectionStart = tb_log.Text.Length;
            tb_log.SelectionLength = 0;
            tb_log.ScrollToCaret();

After this command the old selected text, that I highlighted in green, disappears.

My goal, again, is to keep the background color of the text that i highlighted before and highlight again and again in the future.

1
I have no idea what you're talking about. Show us your code. - João Mendes
thanks a lot @LarsTech!!!!!!!!!!!!!! YOU ARE A GENIUS! :) :) :) - Gasta87

1 Answers

1
votes

The code you posted does not make the lime highlighted text "unhighlight".

I suspect you have:

tb_log.Text += some text;

somewhere in your code. That will replace all of the current formatting. Use

tb_Log.AppendText(some text);

instead to preserve the richness of the format.