富文本框字体变为选定的文本字体

时间:2011-03-15 08:56:09

标签: c# richtextbox

上下文。聊天服务器。

因此我需要用户拥有自己的字体和颜色

我们说目前聊天框现在有两行

Red line
Green line

红色用户输入另一行。整个RichTextBox变为红色。即

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 :(得分:0)

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 + =解决问题。

任何人都知道为什么?

相关问题