【问题标题】:Append rich formatted text from 2 RichTextboxes into another RichTextBox in C#将富格式文本从 2 个 RichTextbox 附加到 C# 中的另一个 RichTextBox
【发布时间】:2014-09-17 05:16:00
【问题描述】:

我有 3 个 RichTextBox:richTextBox1、richTextBox2 和richTextBox3。

我运行应用程序并在文本框 1 和 2 中输入文本。

所以richTextBox1 的文本是“Test”,而richTextBox2 的文本是“ing”。

我现在想将该文本附加在一起并将其放入另一个richTextBox(保留任何格式,如粗体、下划线等...)

所以我尝试以下代码:

richTextBox3.Rtf = richTextBox1.Rtf + richTextBox2.Rtf;

这不会导致任何错误,但我只从richTextBox1 获取文本。所以我得到了“测试”。

那么如何在保持格式的同时复制 2 个 RichTextBox 的内容呢?

【问题讨论】:

    标签: c#-4.0 richtextbox rtf


    【解决方案1】:

    你会想要这样做:

    richTextBox3.Rtf = richTextBox1.Rtf
    richTextBox3.Select(richTextBox3.TextLength, 0);
    richTextBox3.SelectedRtf = richTextBox2.Rtf;
    

    这应该可以解决问题。

    【讨论】:

    • 谢谢。根据需要工作。
    【解决方案2】:

    用法:

    richTextBox3.Rtf = MergeRtfTexts(new RichTextBox[] { richTextBox1, richTextBox2});
    

    RTF合并功能:

        private string MergeRtfTexts(RichTextBox[] SourceRtbBoxes)
        {
            using (RichTextBox temp = new RichTextBox())
            {
                foreach (RichTextBox rtbSource in SourceRtbBoxes)
                {
                    rtbSource.SelectAll();
                    //move the end
                    temp.Select(temp.TextLength, 0);
                    //append the rtf
                    temp.SelectedRtf = rtbSource.SelectedRtf;
                }
                return temp.Rtf;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2011-07-27
      相关资源
      最近更新 更多