C# Bold a part of a string in a RichTextBox

L337BEAN picture L337BEAN · Apr 30, 2012 · Viewed 10.7k times · Source

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

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.

Answer

L337BEAN picture L337BEAN · May 1, 2012

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!

: Dan