更改文本框前一行文本的颜色 [英] Change text box fore color of one line of text
本文介绍了更改文本框前一行文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法可以让一行文字说红色和其他绿色?
Is there a way I can make one line of text say red and other green?
if (Process.GetProcessesByName("RobloxPlayerBeta").Length > 0)
{
System.Threading.Thread.Sleep(4000);
textBox2.Text += "\r\nWelcome to AL7!";
textBox2.Update();
textBox2.Text += "\r\nLoading Lua environment.";
textBox2.Update();
textBox2.Text += "\r\nERROR: Lua state failed.";
textBox2.Update();
我尝试过:
What I have tried:
if (Process.GetProcessesByName("RobloxPlayerBeta").Length > 0)
{
System.Threading.Thread.Sleep(4000);
textBox2.Text += "\r\nWelcome to AL7!";
textBox2.Update();
textBox2.Text += "\r\nLoading Lua environment.";
textBox2.Update();
textBox2.Text += "\r\nERROR: Lua state failed.";
textBox2.Update();
推荐答案
嗨会员12896541,
字面上你想要的是TextBox无法实现的。如果您确实需要该颜色机制,则需要使用富文本框。答案已经在这里给出: c# - 每行的RichTextBox不同的背景颜色 - Stack溢出 [ ^ ]
RichTextBox控件具有Lines
枚举器,可以对其进行操作以获取每个引用在框中排队。然后,您需要使用GetFirstCharIndexFromLine
和行长度属性选择整行。最后,您需要一种机制来为每个奇数和偶数行设置颜色;你可以使用模数运算符。
Hi Member 12896541,
Literally what you want is not achievable by TextBox. If you really need that color mechanism, you need to use a rich text box. The answer is already given here: c# - RichTextBox different back color for each line - Stack Overflow[^]
A RichTextBox control hasLines
enumerator that can be manipulated to get a reference to each line in the box. Then you need to select the entire line using theGetFirstCharIndexFromLine
and the line length property. Finally you need a mechanism to set color for every odd and even line; you can use a modulus operator.
int LineCount = 0;
foreach (string line in richTextBox1.Lines)
{
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(LineCount), line.Length);
richTextBox1.SelectionColor = (LineCount++ % 2 == 0) ? Color.Red : Color.Green;
}
请在此查看:
RichTextBox.SelectionColor属性(System.Windows.Forms) [ ^ ]
另请查看Philippe提到的5分。这些是很好的建议;所以请相应地更改代码。
Please check here as a reference:
RichTextBox.SelectionColor Property (System.Windows.Forms)[^]
Also check the 5 points mentioned by Philippe. These are good suggestions; so please change the code accordingly.
这篇关于更改文本框前一行文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文