所以我有一串通常采用这种格式的字符:
“ t e xt”,粗体为红色。
我想在RichTextBox中以bold字符为红色显示这些类型的字符串。
我在一个字符串变量中有文本,而我在一个int变量中有红色字符(在字符串中)的位置。
我的解决方法是:
这是我到目前为止所拥有的:
https://github.com/icebbyice/rapide/blob/master/rapide/SpreadWindow.xaml.cs
查找:“ // stackoverflow”(同样,separateOutputs未完成)
我停在那里是因为我认为必须有一种更有效的方法来完成它,因为我将经常更改富文本框的内容(最多更改1000个内容/ 60秒)。
所以,有更好的方法吗?
您可以执行以下操作://获取关键字/函数字符串关键字= @“ \ b(e)\ b”;MatchCollection keywordMatches = Regex.Matches(codeRichTextBox.Text,关键字);
// saving the original caret position + forecolor
int originalIndex = codeRichTextBox.SelectionStart;
int originalLength = codeRichTextBox.SelectionLength;
Color originalColor = Color.Black;
// MANDATORY - focuses a label before highlighting (avoids blinking)
menuStrip1.Focus();
// removes any previous highlighting (so modified words won't remain highlighted)
codeRichTextBox.SelectionStart = 0;
codeRichTextBox.SelectionLength = codeRichTextBox.Text.Length;
codeRichTextBox.SelectionColor = originalColor;
// scanning...
foreach (Match m in keywordMatches)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionFont = new Font(codeRichTextBox.Font, FontStyle.Bold);
}
// restoring the original colors, for further writing
codeRichTextBox.SelectionStart = originalIndex;
codeRichTextBox.SelectionLength = originalLength;
codeRichTextBox.SelectionColor = originalColor;
codeRichTextBox.SelectionFont = new Font(codeRichTextBox.Font, FontStyle.Regular);
// giving back the focus
codeRichTextBox.Focus();
属于RichTextBox
TextChanged
Event
如果输入e,它将显示为Bold。其他任何文字将显示为Font.Regular
这就是我所拥有的,希望对您有帮助:)