RichTextBox中的颜色​​输出字符串

我正在研究一个调用python脚本并在richtextbox中显示输出的程序。 我在输出中着色特定字符串时遇到问题。 例如:如果字符串“hey 123”将出现在输出中,它将显示为红色。 有人有答案吗? 谢谢! 我的代码片段:

Process _cmd; delegate void setTextCallBack(string text); private void setText (string text) { if (this.richTextBox1.InvokeRequired) { setTextCallBack d = new setTextCallBack(setText); this.Invoke(d, new object[] { text }); } else { this.richTextBox1.Text += text + Environment.NewLine; richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.ScrollToCaret(); } } 
 _cmd = new Process(); _cmd.StartInfo = cmdStartInfo; if (_cmd.Start()) { _cmd.OutputDataReceived += new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.ErrorDataReceived += new DataReceivedEventHandler(_cmd_ErrorDataReceived); _cmd.Exited += new EventHandler(_cmd_Exited); _cmd.BeginOutputReadLine(); _cmd.BeginErrorReadLine(); } else { _cmd = null; } 
 void _cmd_Exited(object sender, EventArgs e) { _cmd.OutputDataReceived -= new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.Exited -= new EventHandler(_cmd_Exited); } void _cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data, Brushes.Red); } void _cmd_OutputDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data, Brushes.Green); } private void UpdateConsole (string text) { UpdateConsole(text, null); } private void UpdateConsole(string text, Brush color) { WriteLine(text, color); } private void WriteLine(string text, Brush color) { if (text != null) { setText(text); } } 

这是RichTextBoxes黄金法则不要触摸文本!

完成任何着色或格式化后,请勿使用richTextBox1.Text += "more text"附加文本! 这会搞乱你的所有格式。 您需要的所有function都有特殊function,您需要使用它们。

添加用途

 richTextBox1.AppendText("more text") 

编辑时可能需要的其他方法是Copy, Paste & Cut以及选择属性和方法,您还需要着色和格式化。

要为一段文字着色,您需要选择它然后应用您想要的颜色:

 richTextBox1.SelectionBackColor = Color.MistyRose; // Backcolor richTextBox1.SelectionColor = Color.Red; // TextColor 

其他格式将使用SelectionFontSelectionIndentSelectionAlignment ,然后一些..

正如你所看到的那样,所有工作都是先选择然后再进行选择。

在您的代码中,您可以将setText方法标题更改为:

 setText (string text, Color color) 

并改变它的代码:

 else { int start = richTextBox1.Text.Length; //* richTextBox1.AppendText(text + Environment.NewLine); richTextBox1.SelectionStart = start; //* richTextBox1.SelectionLength = text.Length; richTextBox1.SelectionColor = color; richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.SelectionLength = 0; richTextBox1.ScrollToCaret(); } 

最后三行是可选的; 我把它们包括在内,因为你似乎总是想要Caret。

我还可以通过将函数的名称更改为addTextaddColoredText来使其更具可读性,甚至可以通过添加bool参数来控制是否应添加NewLine来使其更加灵活。

更新

我注意到你也在使用Brushesnull调用UpdateConsoleWriteLine方法。 这有两个问题:

  • 我们不需要使用Brush而是使用Color来为RichTextBox着色。
  • 并且您不能为Color传入null

你可以通过声明它们是SolidBrushes并使用它们的brush.Color来解决这个brush.Color 。 但这只是一种无用且令人困惑的扭曲。

而是直接传递你想要的Color ,而不是Black或RTF的ForeColor的null传递或你定义的默认颜色..:

 UpdateConsole(text, Color.Black); 

要么

 UpdateConsole(text, richTextBox1.ForeColor); 

等等..

更新2

请注意代码中的更正(*); AppendText移动SelectionStart,因此我们需要存储起始值。

如果您有错误消息列表,请说

 List errors; 

你可以从文件填充它,也许是这样的:

 errors = File.ReadAllLines("d:\\errors.txt").ToList(); 

然后要么测试平等:

 setText(text, errors.Contains(text) ? Color.Red : Color.Blue); 

或者,如果只有零件匹配

 setText(text, isError(text) ? Color.Red : Color.Blue); 

使用这样的函数:

 bool isError(string msg) { foreach (string err in errors) if (msg.IndexOf(err) >= 0) return true; return false; }