【问题标题】:A new FontStyle is applied to different locations in RichTextBox新的 FontStyle 应用于 RichTextBox 中的不同位置
【发布时间】:2012-12-12 13:04:57
【问题描述】:

我正在尝试编写一个自动格式化参考书目的程序,它使用以下模式:

int ct = 0;
private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.Text += ct.ToString() + textBox1.Text;
    richTextBox1.Text += textBox2.Text;
    richTextBox1.Text += "\n\r";
    ct = ct + 1;
}

效果很好,问题是我希望第二个 textBox 中的文本为斜体。当我尝试使用时

richTextBox1.Find(textBox2.Text, RichTextBoxFinds.MatchCase);
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);

那么只有最后一个“textBox2.Text”是斜体的;以前的被重置为常规字体。如果有人能告诉我如何解决这个问题,我将不胜感激,谢谢。

【问题讨论】:

    标签: c# richtextbox selection


    【解决方案1】:

    我使用 StringReader 和第二种形式找到了解决方案:

        int ct = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\n\r";
            richTextBox1.Text += ct.ToString()+". "+textBox1.Text;
            richTextBox1.Text += ", " + textBox2.Text;
            form2.richTextBox1.Text += textBox2.Text + "\n";
            ct = ct + 1;
            using (StringReader reader = new StringReader(form2.richTextBox1.Text))
            {
                for (int i = 0; i < ct; i++)
                {
                    string A = reader.ReadLine();
                    richTextBox1.Find(A, RichTextBoxFinds.MatchCase);
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      相关资源
      最近更新 更多