Click here to Skip to main content
14,639,650 members
Rate this:
Please Sign up or sign in to vote.
See more:
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();
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100