富文本框如何突出显示文本块

我需要在RTB中突出显示我的文本的某一部分,而不是在改变字体样式/颜色的意义上,而是在使用特定颜色进行块选择的意义上。 这类似于Visual Studio在调试模式下突出显示一行的方式。

如何使用RTB完成此function,或者更确切地说,它是否可能? 如果不可能,我想听听另一种执行上述任务的方法。

我想你正在寻找ScintillaNET 。

另一方面,如果你想在RTB中自己做这个,那么你可以先使用TextBoxBase.Lines属性找到lineNumber 。 然后 …

 //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; 

是的,您可以使用RichTextBox.SelectionBackColor属性设置RichTextBox选择的BackColor。

 int blockStart = 1; //arbitrary numbers to test int blockLength = 15; richTextBox1.SelectionStart = blockStart; richTextBox1.SelectionLength = blockLength; richTextBox1.SelectionBackColor = Color.Yellow; 

在这里,我创建了CustomRichTextBox来实现这一目标。

这里解释了源代码很长的场景。 如果您感兴趣,那么您可以直接重用此用户控件而无需担心

脚本

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality

源代码:

https://github.com/boobalaninfo/CustomRichTextBoxWithHighligh