0

Form と 2 つの richTextBoxes に基づくチャット アプリケーションがあります。

richTextBox1すべての会話を表示するために
richTextBox_TextToSend使用されます 送信するメッセージを入力するために使用されます

ユーザーがメッセージを入力して Enter ボタンを押すと、入力されたテキストが richTextBox1 に表示されます。

private void button1_Click(object sender, EventArgs e)
    {
        // insert message to database
        if(richTextBox_TextToSend.TextLength>0) {

        string txt = richTextBox_TextToSend.Text;

        // send the typed message
        sendMessage(from,to,task_id,txt);

        // show the typed text in the richTextBox1
        richTextBox1.Text += from+": "+richTextBox_TextToSend.Text+"\n";
        richTextBox_TextToSend.Clear();



        }
    }

文字列型の変数fromは、メッセージの送信者 (アプリケーションを使用しているユーザー) の名前を保持します。

名前のみを太字で表示し、他のテキストを通常のフォント スタイルで表示する方法。メッセージを入力した後、

Chakib Yousfi

: Hello...の代わりに Chakib Yousfi : Hello....が表示され

ます。

どんな助けでも大歓迎です。

ここに画像の説明を入力

4

2 に答える 2

2

このコードを使用してください:

private void button1_Click(object sender, EventArgs e)
{
    // insert message to database
    if(richTextBox_TextToSend.TextLength>0) {

    string txt = richTextBox_TextToSend.Text;
    int start = richTextBox1.TextLength;
    string newMessage = from + ": " + richTextBox_TextToSend.Text + Environment.NewLine;

    // send the typed message
    sendMessage(from,to,task_id,txt);

    // show the typed text in the richTextBox1
    richTextBox1.AppendText(newMessage);
    richTextBox_TextToSend.Clear();

    richTextBox1.Select(start, from.Length);
    richTextBox1.SelectionFont = New Font(richTextBox1.Font, FontStyle.Bold);

    }
}
于 2013-02-17T15:15:56.440 に答える
1

まず、太字にしたいテキストを選択する必要があります。

richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 13;

次に、選択したテキストのスタイルを定義できます。

richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
于 2013-02-17T14:18:35.553 に答える