C# forms rich text box cannot change selected text color

Matthew Jeschke 20 Reputation points
2025-07-16T23:52:20.4966667+00:00

I'm designing a form that populates a rich text box from a text file. Certain lines in the text box have to be of a different style (different color and weight). I looked at numerous examples and think I have coded it correctly but whatever I do the style never updates / changes for the desired lines. It's always the default font from the form designer.

Any ideas why I cannot get the "SelectedText" method for Rich Text Box component to work?

Thanks!

Here's the code I am calling to populate the rich text box component:

        `this.rtbTraysTested.Text = "";`

foreach (string uid in ticket.TraysTested().Keys)

{

// Append Tray UID to the text box and select it

this.rtbTraysTested.SelectionStart = this.rtbTraysTested.Text.Length;

this.rtbTraysTested.Text += uid;

this.rtbTraysTested.SelectionLength = uid.Length;

// Text settings

if (ticket.TraysTested()[uid] > 1) // Highlight entry if tray has been tested more than once

{

this.rtbTraysTested.SelectionColor = Color.OrangeRed;

this.rtbTraysTested.SelectionFont = new Font(this.rtbTraysTested.Font, FontStyle.Regular);

}

else

{

this.rtbTraysTested.SelectionColor = Color.White;

this.rtbTraysTested.SelectionFont = new Font(this.rtbTraysTested.Font, FontStyle.Regular);

}

this.rtbTraysTested.Text += Environment.NewLine; // Add the endline to entry

this.rtbTraysTested.Select(0, 0); // Deselect the text

}

Windows development | Windows App SDK
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 123.6K Reputation points
    2025-07-17T00:36:02.8566667+00:00

    Try to use AppendText instead if “+=”:

    // this.rtbTraysTested.Text += uid; -- REMOVED
    int len = this.rtbTraysTested.Text.Length;
    this.rtbTraysTested.AppendText( uid );
    this.rtbTraysTested.SelectionStart = len;
    

    In the second case, use this.rtbTraysTested.AppendText( Environment.NewLine ).

    0 comments No comments

  2. Matthew Jeschke 20 Reputation points
    2025-07-17T17:31:18.8733333+00:00

    Hey thanks you have helped on a couple of my posts. I really appreciate it.

    Got it working. I did as you suggested and also specified the length of the text in selection length.

    this.rtbTraysTested.Text = "";
    foreach (string uid in ticket.TraysTested().Keys)
    {
    	// Append Tray UID to the text box and select it     
        int len = this.rtbTraysTested.Text.Length;
    	this.rtbTraysTested.AppendText(uid);    
    	this.rtbTraysTested.SelectionStart = len;
    
    	// ************************************************************************
    	//  Adding this line in addition to your suggested usage worked! Thanks!
        this.rtbTraysTested.SelectionLength = uid.Length;
    	// *************************************************************************
    
    	// Text settings
    	if (ticket.TraysTested()[uid] > 1)  // Highlight entry if tray has been tested more than once
    	{
    		this.rtbTraysTested.SelectionColor = Color.OrangeRed; 									
    		this.rtbTraysTested.SelectionFont = new Font(this.rtbTraysTested.Font, FontStyle.Regular); 	
    	}
        else
        {
    		this.rtbTraysTested.SelectionColor = Color.Blue;
            this.rtbTraysTested.SelectionFont = new Font(this.rtbTraysTested.Font, FontStyle.Regular); 	
    	} 
    
    	this.rtbTraysTested.AppendText(Environment.NewLine);    // Add the endline to entry
    	this.rtbTraysTested.Select(0, 0);                       // Deselect the text
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.