【问题标题】:C# change color for each line in RichTextBox [duplicate]C#为RichTextBox中的每一行更改颜色[重复]
【发布时间】:2018-06-15 14:24:55
【问题描述】:

我有一个文本,有行,带有“#”。我怎样才能使所有文本变黑,只有这些行变绿?

【问题讨论】:

  • 请在提问前搜索 SO。很可能其他人已经有这个问题了,而且已经回答了多次。
  • 你尝试了什么?包括显示和重现场景所需的代码部分,并描述发生的问题/错误。

标签: c# .net


【解决方案1】:

您可以通过以下方式实现:

String[] lines = ...

foreach (String line in lines)
{
    box.SelectionStart = box.TextLength;
    box.SelectionLength = 0;

    if (line.StartsWith("#"))
        box.SelectionColor = Color.Red;
    else
        box.SelectionColor = Color.Black;

    box.AppendText(line + Environment.NewLine);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 2015-10-06
    • 2014-03-13
    • 1970-01-01
    • 2019-05-09
    • 2010-11-06
    • 2016-09-01
    • 1970-01-01
    相关资源
    最近更新 更多