Adding new line of data to TextBox(向 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 + "
";
}
推荐答案
如果你使用 WinForms:
If you use WinForms:
改用 TextBox 上的 AppendText(myTxt) 方法(.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 添加新的数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 TextBox 添加新的数据行
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
