一个WinForms RichTextBox的范围内更改文本颜色 [英] Change color of text within a WinForms RichTextBox

查看:195
本文介绍了一个WinForms RichTextBox的范围内更改文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我写的每一次点击表单按钮的字符串一个RichTextBox。每个字符串以字符串长或短,并以换行符结束。每次我添加一个字符串,其追加到RichTextBox的底部。我想每个颜色红色线,如果如果它与短开始它的生命与长和蓝色。我怎样才能做到这一点?

I have a RichTextBox that I write a string to every time I click a Form button. Each string begins with the string "Long" or "Short" and ends with a newline. Each time I add a string, it appends to the bottom of the RichTextBox. I'd like to color each line red if it beings with "Long" and blue if it begins with "Short". How can I do this?

推荐答案

当然,这样你就可以做的是使用SelectionStart,SelectionLength和selectionColor设置属性来做到这一点。它的工作原理相当不错的。

Sure, so what you can do is use the SelectionStart, SelectionLength and SelectionColor properties to accomplish this. It works quite well.

查看有关这些属性的信息这个页面

您可以知道RichTextBox的文本和色彩这是你去的SelectionStart属性设置为当前长度的长度,让你将要添加的字符串的长度,设置SelectionLength,然后设置selectionColor设置为合适。冲洗和重复每添加一个字符串。

You can know the length of the RichTextBox text and color this as you go by setting the SelectionStart property to the current length, get the Length of the string you are going to append, set the SelectionLength and then set the SelectionColor as appropriate. Rinse and repeat for each string added.

int length = richTextBox.TextLength;  // at end of text
richTextBox.AppendText(mystring);
richTextBox.SelectionStart = length;
richTextBox.SelectionLength = mystring.Length;
richTextBox.SelectionColor = Color.Red;

类似的东西。这就是我记得它的工作。

Something like that. That's how I remember it working.

这篇关于一个WinForms RichTextBox的范围内更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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