在列表视图/文本框控件中打印具有不同颜色字符的字符串

时间:2022-11-04 16:03:07

I would like to print the output of an algorithm (C#,.NET 4.0)in a listview control or a multiline textbox(or rich textbox) strings containing some characters which are in a different color.For example:

我想在listview控件或多行文本框(或富文本框)字符串中打印算法(C#,. NET 4.0)的输出,其中包含一些颜色不同的字符。例如:

ABCAAAGGGJHHJK (whole string black)
     AHGGJI (H and I - red)
    BAJMGC  (B -green, J,M,C-red)

I know it is possible to change the text color & font for the whole control, but is it possible to generate it like this? Do you have any suggestions?

我知道可以更改整个控件的文本颜色和字体,但是可以像这样生成它吗?你有什么建议吗?

1 个解决方案

#1


1  

You can't easily do it with a ListBox, unless you're willing to draw the items manually (which is a pain). It would probably be even harder with a TextBox. However, you can do it with a RichTextBox.

你不能轻易地用ListBox来做,除非你愿意手动绘制项目(这很痛苦)。使用TextBox可能会更难。但是,您可以使用RichTextBox执行此操作。

richTextBox.SelectionColor = Color.Black;
richTextBox.AppendText("ABCAAAGGGJHHJK\n");
richTextBox.AppendText("A");
richTextBox.SelectionColor = Color.Red;
richTextBox.AppendText("H");
richTextBox.SelectionColor = Color.Black;
richTextBox.AppendText("GGJ");
richTextBox.SelectionColor = Color.Red;
richTextBox.AppendText("I");
// and so on...

#1


1  

You can't easily do it with a ListBox, unless you're willing to draw the items manually (which is a pain). It would probably be even harder with a TextBox. However, you can do it with a RichTextBox.

你不能轻易地用ListBox来做,除非你愿意手动绘制项目(这很痛苦)。使用TextBox可能会更难。但是,您可以使用RichTextBox执行此操作。

richTextBox.SelectionColor = Color.Black;
richTextBox.AppendText("ABCAAAGGGJHHJK\n");
richTextBox.AppendText("A");
richTextBox.SelectionColor = Color.Red;
richTextBox.AppendText("H");
richTextBox.SelectionColor = Color.Black;
richTextBox.AppendText("GGJ");
richTextBox.SelectionColor = Color.Red;
richTextBox.AppendText("I");
// and so on...