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

发布于 2024-10-21 18:01:46 字数 918 浏览 0 评论 0原文

语境。聊天服务器。

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

假设目前聊天框有 2 行

Red line
Green line

,红色用户在另一行中键入。整个 RichTextBox 变成红色。即

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

这是我向 RichTextBox 添加新行的函数。 String 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;
        }

知道出了什么问题吗? String s 表明它确实只选择了换行符。

Context. Chat server.

As such I need the users to have his own font and Colour

Let's say currently the chatbox has 2 lines now

Red line
Green line

And red user types in another line. The whole RichTextBox becomes red. i.e

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

This is my function to add a new line to the RichTextBox. String s is for debugging purpose

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;
        }

Any idea what's wrong? String s shows that it did indeed select only the newline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-10-28 18:01:46
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 += 解决了问题。

有人知道为什么吗?

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;
        }

Apparently using textBox.AppendText instead of textBox.Text += solves the problem.

Anyone know why?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文