向TextBox添加新的数据行 [英] Adding new line of data to TextBox

查看:552
本文介绍了向TextBox添加新的数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个聊天客户端,当前我有一个按钮,单击该按钮将在多行文本框中显示数据。这是向多行文本框中添加数据的唯一方法吗?我觉得这效率很低,因为如果对话时间很长,字符串也会变得很长。

I'm doing a chat client, and currently I have a button that will display data to a multi-line textbox when clicked. Is this the only way to add data to the multi-line textbox? I feel this is extremely inefficient, because if the conversation gets really long the string will get really long as well.

private void button1_Click(object sender, EventArgs e)
        {
            string sent = chatBox.Text;
            displayBox.Text += sent + "\r\n";

        }


推荐答案

如果使用WinForms:

If you use WinForms:

TextBox AppendText(myTxt)方法c $ c>(.net 3.5 +):

Use the AppendText(myTxt) method on the TextBox instead (.net 3.5+):

    private void button1_Click(object sender, EventArgs e)
    {
        string sent = chatBox.Text;

        displayBox.AppendText(sent);
        displayBox.AppendText(Environment.NewLine);

    }

文本本身的内存占用通常较低(您可以在f.ex. 10kb之内说很多,什么都没有)。 TextBox不会渲染缓冲区中的所有文本,仅渲染可见部分,因此您无需过多担心延迟。较慢的操作是插入文本。附加文本相对较快。

Text in itself has typically a low memory footprint (you can say a lot within f.ex. 10kb which is "nothing"). The TextBox does not render all text that is in the buffer, only the visible part so you don't need to worry too much about lag. The slower operations are inserting text. Appending text is relatively fast.

如果您需要更复杂的内容处理,可以使用 StringBuilder 与文本框。这将为您提供一种非常有效的文本处理方式。

If you need a more complex handling of the content you can use StringBuilder combined with the textbox. This will give you a very efficient way of handling text.

这篇关于向TextBox添加新的数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆