Winforms - Underline part of text to be displayed in textbox

c#, winforms

Solution

If you want to show the text using a rich text box, you could do something like this:

richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Underline);
richTextBox1.SelectedText = "Message:";
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Regular);
richTextBox1.SelectedText = " This is a message for Name of Client.";

Or, if the message is dynamic and the header and text are always separated by a colon, you could do something like this:

string message = "Message: This is a message for Name of Client";
string[] parts = message.Split(':');
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Underline);
richTextBox1.SelectedText = parts[0] + ":";
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Regular);
richTextBox1.SelectedText = parts[1];

Or, if you want to show the text dynamically in labels, you could do something like this:

string message = "Message: This is a message for Name of Client";
string[] parts = message.Split(':');

Label heading = new Label();
heading.Text = parts[0] + ":";
heading.Font= new Font("Times New Roman", 10, FontStyle.Underline);
heading.AutoSize = true;
flowLayoutPanel1.Controls.Add(heading);

Label message = new Label();
message.Text = parts[1];
message.Font = new Font("Times New Roman", 10, FontStyle.Regular);
message.AutoSize = true;
flowLayoutPanel1.Controls.Add(message);

Problem

I have a line of text to display and what I want to do is underline only the heading portion of the text in the display. How do I accomplish this please? Message: This is a message for Name of Client. Where "Message:" is underlined.

Original source