c# - RichTextBox 颜色选中的行

标签 c# .net winforms richtextbox

我是 Windows 窗体的新手。我正在使用 VS 2008,C# 编写一个 RichTextBox。 我希望能够在写入 RichTextBox 时用不同的颜色为每一行着色。有人可以指点我 sample 吗? 谢谢

foreach (string file in myfiles)
{
  // As I process my files
  // richTextBox1.Text += "My processing results";
  if(file == "somefileName")
  {
    // Color above entered line or enter new colored line
  }

}

最佳答案

在追加之前设置SelectionColor,类似于:

    int line = 0;
    foreach (string file in myfiles)
    {
        // Whatever method you want to choose a color, here
        // I'm just alternating between red and blue
        richTextBox1.SelectionColor = 
            line % 2 == 0 ? Color.Red : Color.Blue;

        // AppendText is better than rtb.Text += ...
        richTextBox1.AppendText(file + "\r\n");
        line++;
    }

关于c# - RichTextBox 颜色选中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/578813/

相关文章:

c# - 如何扩展 MVC 4 Web Api 和 Entity Framework

c# - C# 和 VBA 之间的通信

c# - 当通过 C# 调用 C++ DLL 的导出类时,该类的 C 样式字符串成员在一个导出函数中是 OK,但在另一个函数中不是

c# - 将 SqlConnection 传递给类会在处理后丢失其连接字符串

.net - 防止 DataGridView 选择第一项

c# - 如何在动态 `textboxes` 上创建事件

c# - 当表中有记录时,如何使用azure向用户组发送电子邮件通知

.net - VB.NET 如何从 SQL 查询的文本中获取结果

c# - 刷新一个控件

c# - RuntimeMethodInfo 相等 : bug?