问题
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.
The important thing here is to achieve the mentioned feature WITHOUT using richtextbox.select function, because the richtextbox I have is being periodically updated and if it calls the select function on each update, the user will have a difficult time dragging the text and I don't want that to happen.
I've heard a solution somewhere along the line of editing the rtf but I'm not entirely certain how to do this. I'd appreciate any help given.
回答1:
EDIT: Just realized question is for winforms. The following answer is for WPF, rather than delete I'm gonna leave it, in case someone find it useful.
Get the text using TextRange(...) and apply Background for example:
TextRange tr = new TextRange(position, nextPosition);
var Br = new SolidColorBrush(Color.FromScRgb(alpha, 1f, 1f, 0f));
tr.ApplyPropertyValue(TextElement.BackgroundProperty, Br);
But perhaps you should look into your update mechanism and come up with a better solution.
回答2:
This does not use the Select() function of the richtextbox. It just uses the start and end index
of the selection and updates the region
in those co-ordinates with
the appropriate colour
.
// change the co-ordinates as per the selection in the run-time
richTextBox1.Text = "Select some text";
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 4;
richTextBox1.SelectionBackColor = Color.LightBlue;
Sele
will be selected in Light Blue
for the above code.
来源:https://stackoverflow.com/questions/11186154/rich-text-box-how-to-highlight-text-block-without-select