1

この文字列の一部を太字に"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 = "";

    }

どんな助けでもこれで素晴らしいでしょう。

4

2 に答える 2

5

この問題は、コメント内の@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");
    }

これが同様の問題を抱えている人に役立つことを願っています!

:ダン

于 2012-05-01T07:51:18.150 に答える
1

これを試してください: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-P​​arsing-the

私はそれを使ったと思います、どれを覚えていませんが、私は成功しました。

于 2012-04-30T13:06:49.277 に答える