简体   繁体   中英

Deselect text in richtextBox without losing highlighted text c#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM