[英]C# Back color on text in a RichTextBox Control
I've created a Windows Form App that reads in a fixed-width text file, gets the width of the columns from user input, then uses that to display the different columns on screen with different text colours. 我创建了一个Windows Form App,该应用程序读取固定宽度的文本文件,从用户输入中获取列的宽度,然后使用它以不同的文本颜色在屏幕上显示不同的列。 It works fine that way.
这样很好。
But, it doesn't work if the field is blank - which in the text files that I'm using, they sometimes are; 但是,如果该字段为空白,则不起作用-在我使用的文本文件中,有时它们是空白; but I still want them to be defined.
但我仍然希望对它们进行定义。 So I thought the best way to do it would be to put a back color on it, like this:
因此,我认为最好的方法是在其上添加底色,如下所示:
http://tinypic.com/r/2ic38sm/7 http://tinypic.com/r/2ic38sm/7
But I can't find how to do it with a RichTextBox - Is it possible? 但是我找不到如何使用RichTextBox进行操作-可以吗? And if not, is there any way I could do it in a Windows form?
如果没有,我可以用Windows形式进行任何操作吗?
Thanks in advance! 提前致谢!
You can use: 您可以使用:
RichTextBox1.SelectAll();
RichTextBox1.SelectionBackColor = Color.Yellow;
If you are looking at changing the colours of the text, then you can use: 如果要更改文本的颜色,则可以使用:
RichTextBox1.SelectionColor = Color.Red;
Here is a useful link: RichTextBox Tips 这是一个有用的链接: RichTextBox提示
Taken from the link above: 摘自上面的链接:
richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold);
richTextBox1.BackColor = Color.AliceBlue;
string[] words =
{
"Dot",
"Net",
"Perls",
"is",
"a",
"nice",
"website."
};
Color[] colors =
{
Color.Aqua,
Color.CadetBlue,
Color.Cornsilk,
Color.Gold,
Color.HotPink,
Color.Lavender,
Color.Moccasin
};
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
Color color = colors[i];
{
richTextBox1.SelectionBackColor = color;
richTextBox1.AppendText(word);
richTextBox1.SelectionBackColor = Color.AliceBlue;
richTextBox1.AppendText(" ");
}
}
Isn't there a Selection.BackColor property on the richTextBox? richTextBox上没有Selection.BackColor属性吗?
Wouldn't a GridView or ListView with Details View be a better control for your problem? GridView或带有Details View的ListView不会更好地解决您的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.