如何向 multi-line 添加一行文本TextBox
?
例如伪代码;
textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");
或
textBox1.Lines.Append("brown");
textBox1.Lines.Append("brwn");
textBox1.Lines.Append("brn");
textBox1.Lines.Append("brow");
textBox1.Lines.Append("br");
textBox1.Lines.Append("brw");
textBox1.Lines.Append("brwm");
textBox1.Lines.Append("bron");
textBox1.Lines.Append("bwn");
textBox1.Lines.Append("brnw");
textBox1.Lines.Append("bren");
textBox1.Lines.Append("broe");
textBox1.Lines.Append("bewn");
唯一的方法 TextBox.Lines实现(我可以看到)是:
- 克隆
- 复制到
- 等于
- 获取类型
- 获取哈希码
- 获取枚举器
- 初始化
- 获取下界
- 获取上限
- 获取长度
- 获取长长度
- 获取值(value)
- 设置值
- 到字符串
最佳答案
@Casperah 指出我的想法是错误的:
TextBox
没有 行- 它有文本
- 如果需要,文本可以在 CRLF 上拆分成行
- 但没有线 的概念
接下来的问题是如何完成我想要的,而不是 WinForms 让我完成的。
在其他给定的变体中存在细微的错误:
textBox1.AppendText("Hello"+ Environment.NewLine);
textBox1.AppendText("Hello"+ "\r\n");
textBox1.Text += "Hello\r\n"
textbox1.Text += System.Environment.NewLine + "brown";
当(可能)不需要换行符时,它们会附加或预先添加换行符。
所以,扩展助手:
public static class WinFormsExtensions
{
public static void AppendLine(this TextBox source, string value)
{
if (source.Text.Length==0)
source.Text = value;
else
source.AppendText("\r\n"+value);
}
}
现在:
textBox1.Clear();
textBox1.AppendLine("red");
textBox1.AppendLine("green");
textBox1.AppendLine("blue");
和
textBox1.AppendLine(String.Format("Processing file {0}", filename));
注意:任何代码都会发布到公共(public)领域。无需注明出处。
关于c# - 如何向多行文本框添加一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8536958/