RichTextBox中的颜色​​输出字符串 [英] Color output strings in RichTextBox

查看:127
本文介绍了RichTextBox中的颜色​​输出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序调用python脚本并在richtextbox中显示输出。
我对输出中的特定字符串着色有问题。
例如:如果字符串 hey 123出现在输出中,它将被染成红色。
有人有答案吗?
谢谢!我的代码的
片段:

  Process _cmd; 
代表无效setTextCallBack(string text);

私有无效setText(字符串文本)
{
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 + =新的EventHandler(_cmd_Exited);

_cmd.BeginOutputReadLine();
_cmd.BeginErrorReadLine();
}
else
{
_cmd = null;
}





  void _cmd_Exited(对象发送方,EventArgs e)
{
_cmd.OutputDataReceived-= new DataReceivedEventHandler(_cmd_OutputDataReceived);
_cmd.Exited-=新的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(字符串文本)
{
UpdateConsole(文本,null);
}

private void UpdateConsole(字符串文本,画笔颜色)
{
WriteLine(text,color);
}

private void WriteLine(字符串文本,画笔颜色)
{
if(text!= null)
{
setText(文本);
}
}


解决方案

此处是 RichTextBoxes 黄金规则请勿触摸文本!



完成任何着色或格式设置后,请勿使用 richTextBox1.Text + =更多文本 附加文本!这会弄乱您的所有格式。

添加用途

  richTextBox1.AppendText(更多文本)

编辑时可能需要的其他方法是复制,粘贴和复制;剪切,当然还有选择的属性和方法,还需要进行着色和设置格式。



要对一段文本进行着色需要选择它,然后像这样应用您想要的颜色:

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

其他格式将使用 SelectionFont SelectionIndent SelectionAlignment ,然后再输入一些。.



如您所见,首先选择然后进行选择就可以了。



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

  setText(字符串文本,颜色)

并更改其代码,如下所示:

  else 
{
int start = richTextBox1。文字长度// *
richTextBox1.AppendText(text + Environment.NewLine);
richTextBox1.SelectionStart =开始; // *
richTextBox1.SelectionLength = text.Length;
richTextBox1.SelectionColor =颜色;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectionLength = 0;
richTextBox1.ScrollToCaret();
}

最后三行是可选的;我将它们包括在内,因为您似乎总是想在最后插入插入符号。.



我还可以通过将函数名称更改为类似 addText的方法来提高可读性 addColoredText ,甚至可以通过添加bool参数来控制是否应添加 NewLine 来使其更加灵活。



更新



我注意到您也在呼叫 UpdateConsole WriteLine 方法,其中 null >画笔。
这有两个问题:




  • 我们不需要 Brush ,但使用 Color RichTextBox 着色。

  • 您不能将 null 传递为 Color



您可以通过声明它们为 SolidBrushes 并使用其 brush.Color
但这只是无用和令人困惑的转折。



直接通过您想要的颜色传递,而不是通过 Black <空传递/ code>或RTF的 ForeColor 或您定义的默认颜色。.:

  UpdateConsole(text,Color.Black); 

  UpdateConsole(text,richTextBox1.ForeColor); 

等。



更新2



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



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

  List< string>错误; 

您可以从文件中填充它,也许像这样:

  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(字符串msg)
{foreach(错误字符串err)
if(msg.IndexOf(err)> = 0)返回true;返回false; }


I'm working on a program that calls python script and shows the output in richtextbox. I have a problem with coloring specific strings in output. For example: if the string "hey 123" will appear in the output it will colored red. Does someone have an answer? Thanks! snippet of my code:

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);
    }
}

解决方案

Here is the Golden Rule for RichTextBoxes: Don't touch the Text!

Do not use richTextBox1.Text += "more text" to append Text once you have done any coloring or formatting! This will mess up all formatting you have. There are special functions for everything you need, and you need to use those.

To add use

richTextBox1.AppendText("more text")

Other methods you may need when editing are Copy, Paste & Cut plus of course the select properties and methods, you need for the coloring and formatting as well..

To color a piece of text you need to select it and then apply the Color you want like this:

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

Other formatting will use SelectionFont, SelectionIndent, SelectionAlignment and then some..

As you see it all works by first selecting and then working on that selection.

In your code you could change the setText method header to:

setText (string text, Color color)

and change its code like this:

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();
}

The last three lines are optional; I include them since you seem to want the Caret always at the end..

I would also make it more readable by changing the function's name to something like addText or addColoredText and maybe even make it more flexible by adding a bool parameter to control whether a NewLine should be added or not..

Update:

I notice that you are also calling the UpdateConsole and WriteLine methods with a null for the Brushes. This has two problems:

  • We don't need a Brush but a Color for coloring the RichTextBox.
  • And you can't pass in a null for a Color.

You could workaround by declaring them to be SolidBrushes and the using their brush.Color. But that is just a useless and confusing twist.

Instead pass in the Color you want directly and instead of null pass in Black or the RTF's ForeColor or a default color you define..:

UpdateConsole(text, Color.Black);   

or

UpdateConsole(text, richTextBox1.ForeColor);

etc..

Update 2

Please note the correction (*) in the code; AppendText moves the SelectionStart, so we need to store the start value.

If you have a list of error messages, say

List<string> errors;

You could fill it from a file, maybe like this:

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

And then either test for equality:

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

Or, if only parts match

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

using a function like this:

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

这篇关于RichTextBox中的颜色​​输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆