繁体   English   中英

C#在RichTextBox中将字符串的一部分加粗

[英]C# Bold a part of a string in a RichTextBox

我试图加粗此字符串的"You >>"部分,以使其在富文本框中显示。

以下是我单击消息发送按钮时的代码。 displayBox是id之类的字符,如字符串以粗体显示, entryBox是用户输入消息的位置。

        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 = "";

    }

任何帮助将对此大有帮助。

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

这是我的代码,因为它现在代表相同的按钮(尽管自那以来我已将其重命名)。 这可能不是解决此问题的最佳方法,但我取得了预期的结果,因此对此感到满意。 注释中对此进行了解释。

        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");
    }

希望这可以为任何有类似问题的人提供帮助!

:丹

暂无
暂无

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

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