简体   繁体   English

在文本框中设置文本颜色 C#

[英]Setting text color in text box C#

I have a multi line text box which is used to display the status messages of the operations that I do in the application.我有一个多行文本框,用于显示我在应用程序中执行的操作的状态消息。 I want to set separate colors for different lines in the text box.我想为文本框中的不同行设置单独的 colors 。 If I use the ForeColor property, it sets the color of all the text.如果我使用 ForeColor 属性,它会设置所有文本的颜色。 I want to set the color of a single line.我想设置单行的颜色。 How can I do this?我怎样才能做到这一点?

Thanks, Rakesh.谢谢,拉克什。

You need to use the RichTextBox instead.您需要改用RichTextBox You can then use the properties SelectionStart , SelectionLength , and SelectionColor to achieve what you want, as the easiest solution.然后,您可以使用属性SelectionStartSelectionLengthSelectionColor来实现您想要的,这是最简单的解决方案。 You can also modify the RTF directly.您也可以直接修改 RTF。 You can actually just edit the designer code and change System.Windows.Forms.TextBox references to RichTextBox, then make some small modifications to your code.您实际上可以只编辑设计器代码并将 System.Windows.Forms.TextBox 引用更改为 RichTextBox,然后对您的代码进行一些小的修改。

TextBox control doesn't support coloring of intermediate lines. TextBox 控件不支持中间线的着色。 use ListView instead.改用 ListView。 You can achieve a similar look-and-feel by using "Details" or "List" View mode.您可以使用“详细信息”或“列表”视图模式实现类似的外观。

You can't do that with the built in control.您无法使用内置控件做到这一点。

Either inherit it and create your own control, or use something like this:要么继承它并创建你自己的控件,要么使用这样的东西:
Background color of a ListBox item (winforms) ListBox 项的背景颜色(winforms)

In there it's background color but you can same way apply different fore color.在那里它是背景颜色,但你可以以同样的方式应用不同的前景色。

TextBox does not support multiple colors (or fonts or sizes). TextBox 不支持多个 colors(或 fonts 或大小)。 You'll have to use RichTextBox.您必须使用 RichTextBox。

You can't.你不能。

You need to use Rich TextBox instead.您需要改用富文本框

You have to use a richtextbox for this.为此,您必须使用富文本框。 You can specify all sorts of formatting inline with your text您可以在文本中指定各种格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM