Search code examples
c#winformsrichtextboxrtf

Set prefix to Bold and preserve underline


I am trying to set my title to Bold and also preserve the underline. For example:

TitleScreenshot

The code keeps making all lines bold. As you can see from the image above, only the title should be bold. How can I fix the code to match the image?

        List<string> titles = new List<string> { "Basic Metabolic Panel", "Complete Blood Count", "Comprehensive Metabolic Panel" };
        List<string> bodies = new List<string> { " : Collected: 8/20/2012 5:45:00 PM", " : Collected: 8/20/2012 5:45:00 PM", " : Collected: 8/17/2012 4:18:00 AM" };


        for (int i = 0; i < titles.Count; i++)
        {
            string labName = titles[i];
            string collectionDate = bodies[i];
            int oldTextEndPoint = rtb.Text.Length;

            rtb.Text += labName + collectionDate + Environment.NewLine;

            rtb.Select(oldTextEndPoint, labName.Length + collectionDate.Length);
            rtb.SelectionFont = new Font(rtb.Font, rtb.SelectionFont.Style | FontStyle.Underline);

            rtb.Select(oldTextEndPoint, labName.Length);
            rtb.SelectionFont = new Font(rtb.Font, rtb.SelectionFont.Style | FontStyle.Bold);
        }   

Solution

  • Changed

    rtb.Text += labName + collectionDate + Environment.NewLine;

    to

    rtb.AppendText(labName + collectionDate + Environment.NewLine);

    and things work correctly...