如何在 多行 文本框
?
例如伪代码;
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 实现(我可以看到)是:
The only methods that TextBox.Lines implements (that i can see) are:
@Casperah 指出我想错了:
@Casperah pointed out that i'm thinking about it wrong:
TextBox
没有 行TextBox
doesn't have lines那么问题是如何完成我想要的,而不是 WinForms 让我做什么.
The question then is how to accomplish what i want, rather than what WinForms lets me.
在其他给定的变体中存在细微的错误:
There are subtle bugs in the other given variants:
textBox1.AppendText("Hello" + Environment.NewLine);
textBox1.AppendText(Hello"+
");
textBox1.Text += "Hello
"
textbox1.Text += System.Environment.NewLine + "brown";
textBox1.AppendText("Hello" + Environment.NewLine);
textBox1.AppendText("Hello" + "
");
textBox1.Text += "Hello
"
textbox1.Text += System.Environment.NewLine + "brown";
当(可能)不需要时,它们会附加或添加换行符.
They either append or prepend a newline when one (might) not be required.
所以,扩展助手:
public static class WinFormsExtensions
{
public static void AppendLine(this TextBox source, string value)
{
if (source.Text.Length==0)
source.Text = value;
else
source.AppendText("
"+value);
}
}
那么现在:
textBox1.Clear();
textBox1.AppendLine("red");
textBox1.AppendLine("green");
textBox1.AppendLine("blue");
和
textBox1.AppendLine(String.Format("Processing file {0}", filename));
注意:任何代码都会发布到公共领域.无需署名.
Note: Any code is released into the public domain. No attribution required.
这篇关于如何在多行文本框中添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!