user1031034
user1031034

Reputation: 856

How to add a line to the Text property of a textbox?

I have TextBox in a Windows form application. And I write a text in it.

eg.

texbox.Text = " first line ";
....
textbox.Text = "second line";

When I write second text, the first line is deleted. How to leave the first line and write next texts in next line in the TextBox?

I want the following result:

first line
second line

Upvotes: 0

Views: 2461

Answers (5)

Oliver
Oliver

Reputation: 83

I usually write a wrapper.

One important difference is to use

Environment.Newline 

instead of

"\n\r".  

Also, as others have noted, set the textBox multiline property.

    public void WriteLine(string msg)
    {
        if (!string.IsNullOrEmpty(textBox.Text))
        {
            msg = string.Format("{0}{1}", Environment.NewLine, msg);
        }
        textBox.AppendText(msg);
    }

Upvotes: 1

Brijesh Patel
Brijesh Patel

Reputation: 2958

You can do following ways also.

textbox.text = "first line"; textbox.text = textbox.text + vbCrlf + "second line";

Upvotes: 0

Rahul
Rahul

Reputation: 77876

You can set the Textbox multiline property to true and can use \r\n for multiline text like below:

TextBox1.Text = "First line\r\nSecond line"; 

Upvotes: 0

PraveenVenu
PraveenVenu

Reputation: 8337

You want to change the TextMode property to MultiLine

then you can write like

texbox.text = " first line ";
....
textbox.text += "\nsecond line";

Please note the append operator += and \n which is new line character

Upvotes: 1

Justin
Justin

Reputation: 6549

textbox.text = "first line";
textbox.text += "\nsecond line";

or

textbox.text = "first line";
textbox.text = textbox.text + "\nsecond line";

Upvotes: 1

Related Questions