C# 将 RichTextBox 中字符串的一部分加粗 [英] C# Bold a part of a string in a RichTextBox

查看:121
本文介绍了C# 将 RichTextBox 中字符串的一部分加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此字符串的 "You >>" 部分加粗以显示在富文本框中.

I am trying to bold the "You >>" part of this string to be displayed in a rich text box.

以下是我点击消息发送按钮时的代码.displayBox 是 id 喜欢加粗的字符串的地方,entryBox 是用户输入消息的地方.

The following is my code for when the message send button is clicked. displayBox is where id like the string to be bold, entryBox is where the user is entering a message.

        private void button1_Click(object sender, EventArgs e)
    {
        listData.Add(entryBox.Text);
        // Remove the linebreak caused by pressing return
        SendKeys.Send("\b");


        // Empty the array string
        ArrayData = "";

        // Bold the You >>
        displayBox.SelectionStart = 0;
        displayBox.SelectionLength = 6;
        displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
        displayBox.SelectionLength = 0;

        foreach (string textItem in listData)
        {
            ArrayData = ArrayData + "You >> " + textItem + "\r\n";
        }
        entryBox.Focus();
        displayBox.Text = "";
        displayBox.Refresh();
        displayBox.Text = ArrayData;
        entryBox.Text = "";

    }

对此有任何帮助都会很棒.

Any help would be great on this.

推荐答案

此问题已在评论中@dash 链接的帮助下解决.链接:http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx

This issue was solved with help from @dash's link in the comments. Link: http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx

这是我的代码,因为它现在代表同一个按钮(虽然我已经重命名了它).这可能不是解决这个问题的最干净的解决方案,但我达到了预期的结果,所以我很满意.评论中对此进行了解释.

This is my code as It stands now for the same button (although I have renamed it since). This may not be the cleanest solution to this issue but I achieved the desired result so I'm happy with it. It is explained in the comments.

        private void send_Click(object sender, EventArgs e)
    {
        if (entryBox.Text != "")
        {
            listData.Add(entryBox.Text);
            // Remove the linebreak caused by pressing return
            SendKeys.Send("\b");

            // Empty the array string
            ArrayData = "";

            foreach (string textItem in listData)
            {
                ArrayData = ArrayData + "You >> " + textItem + "\r\n";
            }
            entryBox.Focus();
            displayBox.Text = "";
            displayBox.Refresh();
            displayBox.Text = ArrayData;

            // Format the "You >>"
            displayBox.SelectionStart = 0;
            displayBox.SelectionLength = 6;
            displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
            displayBox.SelectionColor = Color.Crimson;
            displayBox.SelectionLength = 0;
            string wordToFind = "You >>";
            int startIndex = 0;
            while (startIndex > -1)
            {
                startIndex = displayBox.Find(wordToFind, startIndex + 1,
                                displayBox.Text.Length,
                                RichTextBoxFinds.WholeWord);
                if (startIndex > -1)
                {
                    displayBox.Select(startIndex, wordToFind.Length);
                    displayBox.SelectionFont = new Font(displayBox.Font, FontStyle.Bold);
                    displayBox.SelectionColor = Color.Crimson;
                }
            }
            // Reset the entry box to empty
            entryBox.Text = "";

        }
        // Remove the linebreak caused by pressing return
        SendKeys.Send("\b");
    }

我希望这能为遇到类似问题的人提供一些帮助!

I hope this provides anyone with a similar problem some help!

:丹

这篇关于C# 将 RichTextBox 中字符串的一部分加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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