Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rich Text Box how to highlight text block

I need a certain portion of my text in RTB to be highlighted not in the sense of changing the font style/color, but in the sense of making a block selection with a particular color. This is similar to how Visual Studio highlights a line during debug mode.

How can I accomplish this feature using RTB or rather, is it even possible? If it isn't possible, I'd like to hear another way of performing the above task.

like image 476
l46kok Avatar asked Jun 25 '12 04:06

l46kok


People also ask

How do you highlight a block of text?

How to highlight text using your keyboard. To highlight with the keyboard, move to the starting location using the arrow keys. Then, hold down the Shift key, and press the arrow key in the direction you want to highlight. Once everything you want is highlighted, let go of the Shift key.

How do I highlight text in highlighter?

Highlight selected text Select the text that you want to highlight. Go to Home and select the arrow next to Text Highlight Color. Select the color that you want.


2 Answers

Yes you can set the BackColor of a RichTextBox Selection using the RichTextBox.SelectionBackColor Property.

int blockStart = 1; //arbitrary numbers to test
int blockLength = 15;
richTextBox1.SelectionStart = blockStart;
richTextBox1.SelectionLength = blockLength;
richTextBox1.SelectionBackColor = Color.Yellow;
like image 134
Mark Hall Avatar answered Oct 19 '22 00:10

Mark Hall


I think you are looking for ScintillaNET.

On the other hand if you want to do this by yourself in RTB then you can do it by first finding the lineNumber using TextBoxBase.Lines property. Then ...

//Select the line from it's number
startIndex = richTextBox.GetFirstCharIndexFromLine(lineNumber);
richTextBox.Select(startIndex, length);

//Set the selected text fore and background color
richTextBox.SelectionColor = System.Drawing.Color.White;
richTextBox.SelectionBackColor= System.Drawing.Color.Blue;
like image 8
ABH Avatar answered Oct 19 '22 00:10

ABH