簡體   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