l46*_*kok 5 .net c# richtextbox winforms
我需要在RTB中突出显示我的文本的某一部分,而不是在改变字体样式/颜色的意义上,而是在使用特定颜色进行块选择的意义上.这类似于Visual Studio在调试模式下突出显示一行的方式.
如何使用RTB完成此功能,或者更确切地说,它是否可能?如果不可能,我想听听另一种执行上述任务的方法.
是的,您可以使用RichTextBox.SelectionBackColor属性设置RichTextBox选择的BackColor .
int blockStart = 1; //arbitrary numbers to test
int blockLength = 15;
richTextBox1.SelectionStart = blockStart;
richTextBox1.SelectionLength = blockLength;
richTextBox1.SelectionBackColor = Color.Yellow;
Run Code Online (Sandbox Code Playgroud)
我想你正在寻找ScintillaNET.
另一方面,如果你想在RTB中自己做这个,那么你可以先找到lineNumber使用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;
Run Code Online (Sandbox Code Playgroud)