我是一个业余爱好者程序员。我正在制作一个小程序,必须为RichTextBox上的某些特定单词/字符着色。这是我的代码:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int previousSelectionStart = richTextBox1.SelectionStart;
for (int i = 0; i < richTextBox1.Lines[richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)].Length + 1; i++)
{
if (Regex.Match(richTextBox1.Text.Substring(richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)) + i, 1), "[A-Z]").Success)
{
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)) + i, 1);
richTextBox1.SelectionColor = Color.SomeColor;
}
else
{
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)) + i, 1);
richTextBox1.SelectionColor = Color.Black;
}
richTextBox1.Select(previousSelectionStart, 0);
richTextBox1.SelectionColor = Color.Black;
}
}
它有效但存在问题:插入文本时,选择工件开始显示,如下所示:
如何使它们不可见,类似于.HideSelection,但不会失去焦点(并且不会禁用选择,只是让它透明)? :
(或者,也可以在不做任何选择的情况下使此功能正常工作。)
P.S。:从记事本中截取的截图。