Adding new line of data to TextBox

225,273

Solution 1

Following are the ways

  1. From the code (the way you have mentioned) ->

    displayBox.Text += sent + "\r\n";
    

    or

    displayBox.Text += sent + Environment.NewLine;
    
  2. From the UI
    a) WPF

    Set TextWrapping="Wrap" and AcceptsReturn="True"   
    

    Press Enter key to the textbox and new line will be created

    b) Winform text box

    Set TextBox.MultiLine and TextBox.AcceptsReturn to true
    

Solution 2

I find this method saves a lot of typing, and prevents a lot of typos.

string nl = "\r\n";

txtOutput.Text = "First line" + nl + "Second line" + nl + "Third line";

Share:
225,273
Brandon Ling
Author by

Brandon Ling

Updated on July 08, 2022

Comments

  • Brandon Ling
    Brandon Ling almost 2 years

    I'm doing a chat client, and currently I have a button that will display data to a multi-line textbox when clicked. Is this the only way to add data to the multi-line textbox? I feel this is extremely inefficient, because if the conversation gets really long the string will get really long as well.

    private void button1_Click(object sender, EventArgs e)
            {
                string sent = chatBox.Text;
                displayBox.Text += sent + "\r\n";
    
            }