Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a line to a multiline TextBox?

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"); textBox1.Lines.Add("400-749"); ...snip... textBox1.Lines.Add("40-59"); 

or

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

The only methods that TextBox.Lines implements (that i can see) are:

  • Clone
  • CopyTo
  • Equals
  • GetType
  • GetHashCode
  • GetEnumerator
  • Initialize
  • GetLowerBound
  • GetUpperBound
  • GetLength
  • GetLongLength
  • GetValue
  • SetValue
  • ToString

enter image description here

like image 972
Ian Boyd Avatar asked Dec 16 '11 16:12

Ian Boyd


People also ask

How do you make two lines in a text box?

Click the Display tab. To enable multiple lines of text to be typed in the text box, select the Multi-line check box, and then optionally do one of the following: To prevent users from being able to insert paragraph breaks in the text box by pressing ENTER, clear the Paragraph breaks check box.

Is it possible to Enter more than one line in a TextBox control if yes write the method?

Multiline TextBox By default, a TextBox control accepts input in a single line only. To make it multi-line, you need to set Multiline property to true. By default, the Multiline property is false. When you drag and drop a TextBox control from Toolbox to a Form, you cannot change the height of a TextBox control.

How do I go down a line in an Excel text box?

Microsoft Excel in Windows On all versions of Microsoft Excel for the PC and Windows, the keyboard shortcut Alt + Enter moves to the next line. To use this keyboard shortcut, type text in the cell and when ready for a new line, press and hold down the Alt key, then press the Enter key.


2 Answers

@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.

There are subtle bugs in the other given variants:

  • 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.

like image 88
Ian Boyd Avatar answered Oct 08 '22 13:10

Ian Boyd


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(); 
like image 37
Matt Avatar answered Oct 08 '22 12:10

Matt