【发布时间】:2019-02-20 16:44:22
【问题描述】:
如何在 RichTextBox 中为以 # 开头的行着色,就像 python 中的注释一样。我有这段代码,但它应该只给# 所在的行着色。我的代码在写完一个 # 之后为所有内容着色:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string text = richTextBox1.Text;
if (richTextBox1.Lines.Contains("#") == true)
{
int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();
int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);
richTextBox1.Select(firstcharindex, 10);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.DeselectAll();
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
}
【问题讨论】: