C# 向 TextBox 添加新数据行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13318561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding new line of data to TextBox
提问by Brandon Ling
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";
}
采纳答案by Tilak
If you use WinForms:
如果您使用 WinForms:
Use the AppendText(myTxt)method on the TextBoxinstead (.net 3.5+):
使用替代AppendText(myTxt)方法TextBox(.net 3.5+):
private void button1_Click(object sender, EventArgs e)
{
string sent = chatBox.Text;
displayBox.AppendText(sent);
displayBox.AppendText(Environment.NewLine);
}
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.
文本本身通常具有较低的内存占用(您可以在 f.ex. 10kb 内说很多,这是“没有”)。TextBox 不会渲染缓冲区中的所有文本,只会渲染可见部分,因此您无需过多担心延迟。较慢的操作是插入文本。附加文本相对较快。
If you need a more complex handling of the content you can use StringBuildercombined with the textbox. This will give you a very efficient way of handling text.
如果您需要对内容进行更复杂的处理,可以StringBuilder将其与文本框结合使用。这将为您提供一种非常有效的文本处理方式。
回答by alan
Because you haven't specified what front end (GUI technology) you're using it would be hard to make a specific recommendation. In WPF you could create a listbox and for each new line of chat add a new listboxitem to the end of the collection. This linkprovides some suggestions as to how you may achieve the same result in a winforms environment.
因为您没有指定您使用的前端(GUI 技术),所以很难给出具体的建议。在 WPF 中,您可以创建一个列表框,并为每个新的聊天行添加一个新的列表框项目到集合的末尾。此链接提供了一些关于如何在 winforms 环境中实现相同结果的建议。
回答by Tilak
Following are the ways
以下是方法
From the code (the way you have mentioned) ->
displayBox.Text += sent + "\r\n";or
displayBox.Text += sent + Environment.NewLine;From the UI
a) WPFSet TextWrapping="Wrap" and AcceptsReturn="True"Press Enter key to the textbox and new line will be created
b) Winform text box
Set TextBox.MultiLine and TextBox.AcceptsReturn to true
从代码(你提到的方式)->
displayBox.Text += sent + "\r\n";或者
displayBox.Text += sent + Environment.NewLine;从用户界面
a) WPFSet TextWrapping="Wrap" and AcceptsReturn="True"按 Enter 键到文本框,将创建新行
b) Winform 文本框
Set TextBox.MultiLine and TextBox.AcceptsReturn to true
回答by DanKuz
I find this method saves a lot of typing, and prevents a lot of typos.
我发现这种方法可以节省大量打字时间,并防止出现大量拼写错误。
string nl = "\r\n";
字符串 nl = "\r\n";
txtOutput.Text = "First line" + nl + "Second line" + nl + "Third line";
txtOutput.Text = "第一行" + nl + "第二行" + nl + "第三行";
回答by Luke Lozowski
C# - serialData is ReceivedEventHandlerin TextBox.
C# - serialDataReceivedEventHandler在TextBox.
SerialPort sData = sender as SerialPort;
string recvData = sData.ReadLine();
serialData.Invoke(new Action(() => serialData.Text = String.Concat(recvData)));
Now Visual Studio drops my lines. TextBox, of course, had all the correct options on.
现在 Visual Studio 删除了我的台词。当然,TextBox 具有所有正确的选项。
Serial:
序列号:
Serial.print(rnd);
Serial.( '\n' ); //carriage return

