0

我正在尝试将我的标题设置为粗体并保留下划线。例如:

标题截图

代码不断使所有行加粗。从上图中可以看出,只有标题应该是粗体。如何修复代码以匹配图像?

        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);
        }   
4

2 回答 2

1

问题是您将 SelectionFont 设置为基于 RTB 的默认字体的新字体,而不是基于当前的 SelectionFont。

尝试将其更改为:

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

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

编辑1:

我刚刚使用以下代码创建了一个简单的 Windows 应用程序,它同时使用粗体和下划线:

rtb.Text = "abcdefghijklmnopqrstuvwxyz";
rtb.Select(3, 10);
rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style | FontStyle.Bold);
rtb.Select(3, 5);
rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style | FontStyle.Underline);
于 2012-12-19T17:36:29.887 回答
0

改变了

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

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

一切正常......

于 2012-12-19T17:46:28.527 回答