Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there a way I can make one line of text say red and other green?

C#
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:

C#
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();
Posted
Updated 18-Dec-16 12:29pm
Comments
Philippe Mori 18-Dec-16 17:36pm    
That code does not make any sense :

1) You never want to call Sleep form the UI thread.
2) You almost never have to explicitly call Update() explicitly.
3) Why would you update the text 3 times when it could be done once.
4) Hard-coded strings is generally not a good idea.
5) And your text box is poorly named.

And also, it is very stupid to write twice the same question. The What I tried section is not to repeat the same thing that was written in the question. People that waste other time by making then read the same text twice do not deserve much help.
Philippe Mori 18-Dec-16 17:42pm    
You cannot have multiple colors in standard WinForms TextBox. It is not clear from your question if the text box is only used to display information (read only) or the user could also type in it. If the user does not need to type information, you can use an embedded web browser for example.

1 solution

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 has Lines enumerator that can be manipulated to get a reference to each line in the box. Then you need to select the entire line using the GetFirstCharIndexFromLine 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.
C#
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;
}
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900