C# 富文本框如何突出显示文本块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11183599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rich Text Box how to highlight text block
提问by l46kok
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.
我需要在 RTB 中突出显示文本的某个部分,而不是在更改字体样式/颜色的意义上,而是在使用特定颜色进行块选择的意义上。这类似于 Visual Studio 在调试模式下突出显示一行的方式。
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.
我如何使用 RTB 或更确切地说,甚至可能实现此功能?如果不可能,我想听听执行上述任务的另一种方式。
采纳答案by ABH
I think you are looking for ScintillaNET.
我认为您正在寻找ScintillaNET。
On the other hand if you want to do this by yourself in RTB then you can do it by first finding the lineNumberusing TextBoxBase.Linesproperty. Then ...
另一方面,如果您想在 RTB 中自己执行此操作,则可以通过首先找到lineNumberusing TextBoxBase.Lines属性来执行此操作。然后 ...
//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;
回答by Mark Hall
Yes you can set the BackColor of a RichTextBox Selection using the RichTextBox.SelectionBackColorProperty.
是的,您可以使用RichTextBox.SelectionBackColor属性设置 RichTextBox 选择的背景颜色。
int blockStart = 1; //arbitrary numbers to test
int blockLength = 15;
richTextBox1.SelectionStart = blockStart;
richTextBox1.SelectionLength = blockLength;
richTextBox1.SelectionBackColor = Color.Yellow;
回答by Boobalan
Here I have created CustomRichTextBox to achieve this.
在这里,我创建了 CustomRichTextBox 来实现这一点。
The source code a long with scenario is explained here. If you interested then you can reuse this usercontrol directly without worry about much
此处解释了带有场景的源代码。如果您有兴趣,那么您可以直接重用这个用户控件而无需担心太多
Scenario
设想
source code:
源代码:
https://github.com/boobalaninfo/CustomRichTextBoxWithHighligh
https://github.com/boobalaninfo/CustomRichTextBoxWithHighligh

