How to add a line to a multiline TextBox?

后端 未结 10 567
情话喂你
情话喂你 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 16:19

    The adding of Environment.NewLine or \r\n was not working for me, initially, with my textbox. I found I had forgotten to go into the textbox's Behavior properties and set the "Multiline" property to "True" for it to add the lines! I just thought I'd add this caveat since no one else did in the answers, above, and I had thought the box was just going to auto-expand and forgot I needed to actually set the Mulitline property for it to work. I know it's sort of a bonehead thing (which is the kind of thing that happens to us late on a Friday afternoon), but it might help someone remember to check that. Also, in the Appearance section is the "ScrollBars" property that I needed to set to "Both", to get both horizontal and vertical bars so that text could actually be scrolled and seen in its entirety. So the answer here isn't just a code one by appending Environment.NewLine or \r\n to the .Text, but also make sure your box is set up properly with the right properties.

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

    Try this

    textBox1.Text += "SomeText\r\n" 
    

    you can also try

    textBox1.Text += "SomeText" + Environment.NewLine;
    

    Where \r is carriage return and \n is new line

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

    The "Lines" property of a TextBox is an array of strings. By definition, you cannot add elements to an existing string[], like you can to a List<string>. There is simply no method available for the purpose. You must instead create a new string[] based on the current Lines reference, and assign it to Lines.

    Using a little Linq (.NET 3.5 or later):

    textBox1.Lines = textBox.Lines.Concat(new[]{"Some Text"}).ToArray();
    

    This code is fine for adding one new line at a time based on user interaction, but for initializing a textbox with a few dozen new lines, it will perform very poorly. If you're setting the initial value of a TextBox, I would either set the Text property directly using a StringBuilder (as other answers have mentioned), or if you're set on manipulating the Lines property, use a List to compile the collection of values and then convert it to an array to assign to Lines:

    var myLines = new List<string>();
    
    myLines.Add("brown");
    myLines.Add("brwn");
    myLines.Add("brn");
    myLines.Add("brow");
    myLines.Add("br");
    myLines.Add("brw");
    ...
    
    textBox1.Lines = myLines.ToArray();
    

    Even then, because the Lines array is a calculated property, this involves a lot of unnecessary conversion behind the scenes.

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

    Append a \r\n to the string to put the text on a new line.

    textBox1.Text += ("brown\r\n");
    textBox1.Text += ("brwn");
    

    This will produce the two entries on separate lines.

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