How to add a line to a multiline TextBox?

后端 未结 10 566
情话喂你
情话喂你 2020-12-01 15:32

How can i add a line of text to a multi-line TextBox?

e.g. pseudocode;

textBox1.Clear();
textBox1.Lines.Add(\"1000+\");
textBox1.Lines.Add(\"750-999\         


        
相关标签:
10条回答
  • 2020-12-01 15:58

    You have to use the AppendText method of the textbox directly. If you try to use the Text property, the textbox will not scroll down as new line are appended.

    textBox1.AppendText("Hello" + Environment.NewLine);
    
    0 讨论(0)
  • 2020-12-01 16:01

    I would go with the System.Environment.NewLine or a StringBuilder

    Then you could add lines with a string builder like this:

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

    or NewLine like this:

    textbox1.Text += System.Environment.NewLine + "brown";
    

    Better:

    StringBuilder sb = new StringBuilder(textbox1.Text);
    sb.AppendLine("brown");
    sb.AppendLine("brwn");
    
    textbox1.Text = sb.ToString();
    
    0 讨论(0)
  • 2020-12-01 16:07

    If you know how many lines you want, create an array of String with that many members (e.g. myStringArray). Then use myListBox.Lines = myStringArray;

    0 讨论(0)
  • 2020-12-01 16:13

    @Casperah pointed out that i'm thinking about it wrong. A TextBox doesn't have lines, it has text. That text can be split on the CRLF into lines, if requested - but there is no notion of lines.

    The question then is how to accomplish what i want, rather than what WinForms lets me.

    Other given variants have a subtle bug:

    • textBox1.AppendText("Hello" + Environment.NewLine);
    • textBox1.AppendText("Hello" + "\r\n");
    • textBox1.Text += "Hello\r\n"
    • textbox1.Text += System.Environment.NewLine + "brown";

    They either append or prepend a newline when one (might) not be required.

    So, extension helper:

    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);
       }
    }
    

    So now:

    textBox1.Clear();
    textBox1.AppendLine("red");
    textBox1.AppendLine("green");
    textBox1.AppendLine("blue");
    

    and

    textBox1.AppendLine(String.Format("Processing file {0}", filename));
    

    Note: Any code is released into the public domain. No attribution required.

    0 讨论(0)
  • 2020-12-01 16:15

    Above methods did not work for me. I got the following exception:

    Exception : 'System.InvalidOperationException' in System.Windows.Forms.dll
    

    Turns out I needed to call Invoke on my controls first. See answer here.

    0 讨论(0)
  • 2020-12-01 16:16

    Just put a line break into your text.

    You don't add lines as a method. Multiline just supports the use of line breaks.

    0 讨论(0)
提交回复
热议问题