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 添加新的数据行


- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01