在格式文本框格式文本 [英] Format text in Rich Text Box

查看:133
本文介绍了在格式文本框格式文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何格式化格式文本框的文字像下面的

How Can I format text in Rich Text Box like the following

02/11/2010 - 下午5时一刻 - 亚当:添加2日另一项测试注意事项
  十一月

02/11/2010 - 05:15 PM - Adam: Another test notes added on 2nd November

02/11/2010 - 下午5时14 - Z_kas:测试手记。
                        STAGE改为:N ENQ - 发送报价

02/11/2010 - 05:14 PM - Z_kas: Test Notes. STAGE CHANGED TO: N Enq - Send Quote

02/11/2010 - 下午5时12分 - USER32: 2日加入另一项测试注意事项
  十一月

02/11/2010 - 05:12 PM - user32: Another test notes added on 2nd November

感谢

推荐答案

由别人说是有可能的重复与前面一个问题。但是,请在下面看到一个code片段。你不必让你以改变其格式附加文本的长度,只需设置格式您将追加之前。如果您在文本框中有大量的文字这样(我认为)提供更好的性能。

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.

这将工作,只要有在用户文本框中没有选择,然后奇怪的事情会发生,我无法解释。也许别人可以启发我们吗?同样的问题出现在<一个提出的解决方案href=\"http://stackoverflow.com/questions/2527700/change-color-of-text-within-a-winforms-richtextbox\">http://stackoverflow.com/questions/2527700/change-color-of-text-within-a-winforms-richtextbox.我取代了:你以后过的用户名只是为了让我的code样品的日期时间的东西更容易地工作,这可以很容易地在分割进行修改。

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 http://stackoverflow.com/questions/2527700/change-color-of-text-within-a-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".

       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..
    }

和调用:

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

    }

这篇关于在格式文本框格式文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆