简体   繁体   English

在富文本框中格式化文本

[英]Format text in Rich Text Box

How Can I format text in Rich Text Box like the following 如何在富文本框中格式化文本,如下所示

02/11/2010 - 05:15 PM - Adam: Another test notes added on 2nd November 02/11/2010 - 05:15 PM - Adam:另一个测试笔记于11月2日添加

02/11/2010 - 05:14 PM - Z_kas: Test Notes. 02/11/2010 - 05:14 PM - Z_kas:测试笔记。 STAGE CHANGED TO: N Enq - Send Quote 阶段变为:N Enq - 发送报价

02/11/2010 - 05:12 PM - user32: Another test notes added on 2nd November 02/11/2010 - 05:12 PM - user32: 11月2日增加的另一个测试笔记

Thanks 谢谢

as stated by others there is a possible duplication with an earlier question. 如其他人所述,可能与先前的问题重复。 However, please see a code snippet below. 但是,请参阅下面的代码段。 You don't have to get the length of the text you append in order to change its formatting, just set the format before you append. 您不必获取附加文本的长度以更改其格式,只需在追加之前设置格式即可。 This (i think) gives better performance if you have a lot of text in the textbox. 如果文本框中有大量文本,这(我认为)可以提供更好的性能。

This will work as long as there are no selections in the textbox by the user, then strange things will happen that I cannot explain. 只要用户在文本框中没有选择,这将起作用,然后会发生我无法解释的奇怪事情。 Perhaps someone else can enlighten us? 也许别人可以启发我们? The same problem appears with the solution proposed in Change color of text within a WinForms RichTextBox . 在WinForms RichTextBox更改文本颜色时提出的解决方案也会出现同样的问题。 I replaced the “:” you had after the user name just to get my code sample to work more easily with the DateTime thing, this can be easily modified in the “Split”. 我替换了用户名之后的“:”只是为了让我的代码示例更容易使用DateTime工作,这可以在“拆分”中轻松修改。

       private void AddText(string text)
    {
        string[] str = text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

        if (str.Length == 2)
        {
            richTextBox1.DeselectAll();
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
            richTextBox1.AppendText(Environment.NewLine + str[0] + ";");
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Regular);
            richTextBox1.AppendText(str[1]);
        } // Else?? Well, do something else..
    }

And the call: 电话:

        private void button1_Click(object sender, EventArgs e)
    {
        AddText(DateTime.Now.ToString() + " - Mike; Did something");

    }

An alternative is to use the rtf format: 另一种方法是使用rtf格式:

richTextBox1.Rtf = @"{\rtf1\pc \b 02/11/2010 - 05:15 PM - Adam:\b0 Another test notes added on 2nd November \par \b 02/11/2010 - 05:14 PM - Z_kas:\b0 Test Notes. STAGE CHANGED TO: N Enq - Send Quote\par \b 02/11/2010 - 05:12 PM - user32:\b0 Another test notes added on 2nd November";

see msdn: http://msdn.microsoft.com/en-us/library/aa287595%28v=vs.71%29.aspx http://msdn.microsoft.com/en-us/library/aa140301.aspx#rtfspec_8 请参阅msdn: http//msdn.microsoft.com/en-us/library/aa287595%28v=vs.71%29.aspx http://msdn.microsoft.com/en-us/library/aa140301.aspx# rtfspec_8

"\\b " starts the bold section of text and "\\b0" ends it. “\\ b”启动文本的粗体部分,“\\ b0”结束它。 "\\par" starts new paragraph/line (closest to \\n). “\\ par”开始新的段落/行(最接近\\ n)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM