如何在多行TextBox中添加一行?

Ian*_*oyd 54 c# textbox winforms

如何在 行中添加一行文字TextBox

例如伪代码;

textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");
Run Code Online (Sandbox Code Playgroud)

要么

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");
Run Code Online (Sandbox Code Playgroud)

TextBox.Lines实现的唯一方法(我可以看到)是:

  • 克隆
  • 复制到
  • 等于
  • 的GetType
  • GetHashCode的
  • 的GetEnumerator
  • 初始化
  • GetLowerBound
  • GetUpperBound
  • 对GetLength
  • GetLongLength
  • 的GetValue
  • 设定值
  • 的ToString

在此输入图像描述

Ian*_*oyd 101

@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);
   }
}
Run Code Online (Sandbox Code Playgroud)

所以现在:

textBox1.Clear();
textBox1.AppendLine("red");
textBox1.AppendLine("green");
textBox1.AppendLine("blue");
Run Code Online (Sandbox Code Playgroud)

textBox1.AppendLine(String.Format("Processing file {0}", filename));
Run Code Online (Sandbox Code Playgroud)

注意:任何代码都将发布到公共域中.无需归属.


Mat*_*att 22

我会选择System.Environment.NewLine或者aStringBuilder

然后你可以添加像这样的字符串生成器的行:

StringBuilder sb = new StringBuilder();
sb.AppendLine("brown");
sb.AppendLine("brwn");

textbox1.Text += sb.ToString();
Run Code Online (Sandbox Code Playgroud)

或像这样的NewLine:

textbox1.Text += System.Environment.NewLine + "brown";
Run Code Online (Sandbox Code Playgroud)

更好:

StringBuilder sb = new StringBuilder(textbox1.Text);
sb.AppendLine("brown");
sb.AppendLine("brwn");

textbox1.Text = sb.ToString();
Run Code Online (Sandbox Code Playgroud)


DJ *_*mby 14

\r\n字符串附加到字符串以将文本放在新行上.

textBox1.Text += ("brown\r\n");
textBox1.Text += ("brwn");
Run Code Online (Sandbox Code Playgroud)

这将在单独的行上生成两个条目.

  • 它不起作用,`TextBox`控件不能正确处理`\n`,你必须使用`\ r \n`,请检查自己 (5认同)

Vam*_*msi 5

试试这个

textBox1.Text += "SomeText\r\n" 
Run Code Online (Sandbox Code Playgroud)

你也可以试试

textBox1.Text += "SomeText" + Environment.NewLine;
Run Code Online (Sandbox Code Playgroud)

\r回车在哪里,\n是新线路


Pie*_*ant 5

您必须AppendText直接使用文本框的方法。如果您尝试使用该Text属性,文本框将不会在附加新行时向下滚动。

textBox1.AppendText("Hello" + Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)