Богатый текстовый шрифт превращается в выбранный текстовый шрифт - PullRequest
0 голосов
/ 15 марта 2011

Context. Чат-сервер.

Как таковой, мне нужно, чтобы пользователи имели свой собственный шрифт и цвет

Допустим, сейчас в чате есть 2 строки

Red line
Green line

И красный пользователь печатает в другой строке. Весь RichTextBox становится красным. * 1008 то есть *

Red line
Red line //This line was suppose to be green
Red line

Это моя функция для добавления новой строки в RichTextBox. Строка s предназначена для отладки

void OutBox(RichTextBox textBox, string user, string msg, string strFont, string strColour)
        {
            int start = textBox.TextLength;
            textBox.Text += user + " says: " + msg;
            int end = textBox.TextLength;
            textBox.Select(start, end - start);
            Font font = (Font)fc.ConvertFromString(strFont);
            Color colour = (Color)cc.ConvertFromString(strColour);
            string s = textBox.SelectedText;
            textBox.SelectionFont = font;
            textBox.SelectionColor = colour;
        }

Есть идеи, что случилось? Строка s показывает, что она действительно выбрала только символ новой строки.

1 Ответ

0 голосов
/ 15 марта 2011
void OutBox(RichTextBox textBox, string user, string msg, string strFont, string strColour)
        {
            string s = user + " says: " + msg + "\r";
            textBox.AppendText(s);
            textBox.SelectionStart = textBox.TextLength - s.Length;
            textBox.SelectionLength = s.Length;
            Font font = (Font)fc.ConvertFromString(strFont);
            Color colour = (Color)cc.ConvertFromString(strColour);
            string s1 = textBox.SelectedText;
            textBox.SelectionFont = font;
            textBox.SelectionColor = colour;
        }

Очевидно, использование textBox.AppendText вместо textBox.Text + = решает проблему.

Кто-нибудь знает почему?

...