C# 将富文本框中字符串的一部分加粗

本文关键字:一部分 字符串 文本 | 更新日期: 2023-09-27 18:32:37

我正在尝试将此字符串的"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 = "";
    }

任何帮助都会很棒。

C# 将富文本框中字符串的一部分加粗

这个问题是在评论中@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");
    }

我希望这能为任何有类似问题的人提供一些帮助!

:担

试试这个: http://www.codeproject.com/Articles/37668/Multiple-Colored-Texts-in-RichTextBox-using-C

或者这个:http://www.codeproject.com/Articles/15038/C-Formatting-Text-in-a-RichTextBox-by-Parsing-the

我想我用了一个,不记得是哪个了,但我成功了。