Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

How can I insert a new line in a TextBox?

On Saturday, April 23rd 2005 at 12:00 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.2 with 93 votes)
Advertisement
More C# Resources
To insert a new line in a TextBox, first make sure you have the MultiLine property of the TextBox set to True. Then use \r\n (carriage return and new line), as in the following example:

TextBox1.Text = "First line\r\nSecond line";
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by Jon Mattson on Monday, October 3rd 2005 at 12:24 PM

That doesnt work for me?? Ive tried both and it all shows up on the same line.

by Andrei Pociu on Monday, October 3rd 2005 at 12:33 PM

How very odd. Remember, you must have the MultiLine set to True <u>and</u> <i>\r\n</i> where you want the line brake.

It should normally work with C# and .NET Framework 1.1.

by Jon Mattson on Monday, October 3rd 2005 at 12:59 PM

OOOPs, that does work, but this also works:

The TextBox object contains an Array of strings that make up its multiple lines. To do what would normally look like just a “line feed” or carriage return you must lay it out line by line. Here is the code to do what the first tip does.

string[] newLines = new string[2];

newLines[0]="First line";
newLines[1]="Second line";

TextBox1.Lines= newLines;

by Dinesh Mandal(Nepal) on Friday, November 11th 2005 at 09:18 AM

But apply this method too:
textBox1.Text="First Line" + Environment.NewLine + "Second Line";
It will work.

by Awd eka on Friday, December 23rd 2005 at 11:46 AM

That's not a method though, but it will work, yes.

by Dan Shumaker on Wednesday, January 4th 2006 at 09:33 PM

Where did you get the Environment.NewLine from? Ie. How do you declare the Environment object?

by ProProg on Monday, January 16th 2006 at 07:16 PM

Just Use A List Box And Save time :D
you can use the method of adding items

List.items().add("Hello world")
But
Environment.NewLine is Great

by rajat on Friday, January 27th 2006 at 04:31 AM

Well the above said did not work in my case for asp.net, however i tried appending <br> tag where new line was to eb inserted, and it worked for me.

by rajat on Friday, January 27th 2006 at 05:27 AM

Well the above said did not work in my case for asp.net, however i tried appending "<br"
tag in the string where new line was to be inserted, and it worked for me.

by Rajesh on Wednesday, February 22nd 2006 at 01:02 AM

Environment.NewLine is worked for me, good!!!

by mano Bharath on Friday, February 24th 2006 at 01:37 AM

Well, This is really helped me out a lot. Thanks.

by Kjell on Tuesday, May 9th 2006 at 07:59 AM

Environment.Newline worked in C# 1.1. Thanks!

by gitanshu on Wednesday, May 17th 2006 at 03:32 AM

Environment.Newline Suits best !!!

by Frank on Friday, June 2nd 2006 at 12:57 PM

TextBox1.Text = \"First line\\r\\nSecond line\"; worked fine in the code behind.

by Bouke on Saturday, June 10th 2006 at 11:14 AM

\r works
\n works
\r\n works
Environment.NewLine works

by on Friday, June 30th 2006 at 02:18 AM

http://www.geekpedia.com/captcha.php
Verification value

by Ragubathi on Saturday, July 8th 2006 at 12:41 AM

Thanks to you bcas I got a soln from your site

by Talha on Friday, July 21st 2006 at 01:38 AM

amazing !!!!Environment.NewLine

by on Friday, July 28th 2006 at 06:03 PM

7

by Steven Spielberg on Tuesday, August 15th 2006 at 05:54 AM

\r
\n
\r\n DOES NOT NECESSARILY WORK because this is dependent on the machine's environment settings. hence, Environment.NewLine is most recommended

by Roberto Reff on Tuesday, August 15th 2006 at 04:34 PM

also working are:

---
TextBox1.Text = "First line" + + Environment.NewLine + "Second line";
---

Regards

by Roberto Reff on Tuesday, August 15th 2006 at 04:34 PM

sorry:

---
TextBox1.Text = "First line" + Environment.NewLine + "Second line";
---

Regards

by Awadhiya on Monday, August 21st 2006 at 02:31 PM

Environment.NewLine is great.

by Hitesh on Thursday, August 24th 2006 at 07:14 AM

Thanks a lot

by rich on Monday, September 18th 2006 at 12:12 PM

Thanks - works great for formatting text in MailMessage object.

by tokky on Monday, October 23rd 2006 at 09:02 AM

Thank you for all of your help. I have created the example of the fucntion which gets account list. Adapted for your work.

public string getUserList(string username)
{
int i=0;
string tmpdata,data=\"\";
// create reader & open file
TextReader tr = new StreamReader(\"Account.txt\");

// read text from file
do
{
tmpdata = tr.ReadLine();
if (tmpdata == null) break;
if (i % 4 == 0) data = data + \"Username = \" + tmpdata +Environment.NewLine;
else if (i % 4 == 1) data = data + \"Password = \" + tmpdata + Environment.NewLine;
else if (i % 4 == 2) data = data + \"User Level = \" + tmpdata + Environment.NewLine;
else data = data + \"User Status = \" + tmpdata + Environment.NewLine + Environment.NewLine;
i++;
}while(true);


// close the stream
tr.Close();

return data;
}

by Saad on Friday, November 3rd 2006 at 04:44 AM

Environment.NewLine Works fine in Vb.net 2005 2.0

thanx for help;
\n\r does not work;

by sdfdfdfljlkjlk on Tuesday, November 28th 2006 at 12:22 PM

sdfdsf
dsfds
dsfds

by on Saturday, January 6th 2007 at 07:45 PM

1

by OMG on Sunday, January 7th 2007 at 06:36 PM

Nothing works for me... so frustrating.

by Derek on Tuesday, January 9th 2007 at 09:30 AM

Use Environment.NewLine. This helps make your code more portable.

Make sure that the MultiLine property of the textbox is set to true.

Then it should work...

by fassi on Wednesday, January 24th 2007 at 04:19 PM

Environment.NewLine works perfect

by Bill on Thursday, April 19th 2007 at 09:11 AM

In CE.NET 4.2 Compact Framework 2

\\n - does NOT work
\\r\\n - does work

by Mojo-jojo on Wednesday, April 25th 2007 at 03:57 PM

\"\\n\"

by uhgo on Monday, May 14th 2007 at 01:53 PM

if something does not work, try to set the textBox property multiline=true

by gogo on Thursday, May 24th 2007 at 12:09 PM

thanks a lot \\\"Environment.NewLine \\\"..works perfectly fine.

by on Saturday, June 2nd 2007 at 05:28 PM

1

by Anant on Monday, June 4th 2007 at 05:38 AM

i want to read a perticular line from multiline textbox can any one tell me that..... TY in advance

by Test on Wednesday, June 13th 2007 at 12:52 PM

Environment.NewLine worked fine.
<a href=solodig.com>Free Articles, Games, Discussion, Video</a>

by FedLove on Friday, June 29th 2007 at 02:58 PM

Anant - Here is what you would do...

string[] items = textBox1.Lines;

from here, you could use a foreach loop to iterate through each line...

foreach (string i in items)
MessageBox.Show(i);

or maybe use the Array class to find the indexof a particular item you may be looking for...

Array.IndexOf...

Hope that helps, I'm sure there are even more methods you could use.

by Fiza on Wednesday, July 11th 2007 at 03:24 PM

how do i do the same in a label box? like if i want to display name and adress in ONE label box but on separate lines.

by fdgfd on Monday, July 16th 2007 at 04:02 AM

well Environment.NewLine doent work! well strange, i have a text box with multiline on, \r \n \nl and Environment.NewLine dont work even tho i can see the curser moving to the space i want it to but it doesnt it rewrites over the same text :(


txtsave.Text = countTick.ToString() + lblPressureReading.Text + Environment.NewLine;

txtsave.Text = countTick.ToString() + lblPressureReading.Text + "\r";

txtsave.Text = countTick.ToString() + lblPressureReading.Text + "\n";

txtsave.Text = countTick.ToString() + lblPressureReading.Text + "\nl";

all of thoes should work but for some reason they dont! any advice?

by xtrung on Thursday, July 19th 2007 at 11:29 AM

lolz
use +=

by NaR883 on Friday, August 10th 2007 at 12:29 PM

Yes, use += like xtrung said:
txtsave.Text += countTick.ToString() + lblPressureReading.Text + Environment.NewLine;

OR

txtsave.AppendText(countTick.ToString() + lblPressureReading.Text + Environment.NewLine);

by chelu on Thursday, August 16th 2007 at 04:25 PM

Any ideea how I can add a new line to a HTML input text? Is this possible? I don;t want to use textarea ...

by chelu on Thursday, August 16th 2007 at 04:28 PM

maybe some java script ? I want to use a input text like text area. For example if the user rights more then 10 chars it will auto insert a new line. I've try to set the height and with and scrolls to input text control , but it is making only horizontal scroll ...

by Sunny127 on Tuesday, November 6th 2007 at 08:04 AM

The best way is:
Textbox1.AppendText(Environment.NewLine);
Textbox1.AppendText(\"hello!\");

by manish on Monday, January 7th 2008 at 04:28 AM

here is the code to put newline after every 148 characters .use a multiline textbox.


string s = txtinfo.Text;

int i;
if (s.Length > 148)
{
for (i = 0; i < s.Length; i++)
{
int j = 148;
int l = j * k;

if (l < s.Length)
{

s= s.Insert(l , \"\\n\" );


k = k + 1;
}




}
}

//s.Length = s.Length + \"\\n\".ToString ;
// }


char[] delimiter = \"\\r\\n\".ToCharArray();

// Get the lines in the text box
string[] s2 = s.Trim().Split(delimiter);

// Display the number of lines
Response.Write(s2.Length.ToString() + \" lines.\");

by Abadi on Tuesday, January 8th 2008 at 05:39 PM

Thank you very much

Environment.NewLine works more then perfect for me !! :D

by 777777777 on Wednesday, January 23rd 2008 at 08:02 AM

777777777777777777

by Ghhoste on Saturday, February 2nd 2008 at 01:42 PM

Environment.NewLine is awesome.

by Nikolas on Tuesday, February 12th 2008 at 03:21 PM

Non of the above work in String.Format method...

by Nikolas on Tuesday, February 12th 2008 at 03:25 PM

my bad..sorry

by sand on Tuesday, April 8th 2008 at 05:45 PM

The below coding was great simple to use and worked so easily. I'm ever so grateful. Thanxxxx

Just Use A List Box And Save time :D
you can use the method of adding items

List.items().add("Hello world")

by Alaa Farag on Tuesday, April 22nd 2008 at 02:55 AM

"\r\n" is working fine with me, although, "\n" didn't work!
But, the multiline property must be true befure trying to add a new line.

Another word, try making:
textbox.Text = "first line" + "\r\n" + "second line"........and so on.

Good luck.

by Priya on Tuesday, September 9th 2008 at 10:33 PM

Need Help!! with C#. I'm totally new to this programming language.

I have a textbox and it contains a very large amount of numerical value for from a file. each number is space delimited.

I want to treat the value in a textbox as an array and I need to extract from this textbox few values with respect to the index to another textbox.

eg: values in textbox1 (just an example)

120 230 420 100 70 20 89 92 48

i need even values from this.
i basically need to treat this as an array and need to get value by index.

textbox1.array.index[2] = textbox2.text

something like this...

Please help me...

by warren on Wednesday, December 17th 2008 at 09:15 PM

Need Help!! with C#. I'm totally new to this programming language.

I have a textbox and it contains a very large amount of numerical value for from a file. each number is space delimited.

I want to treat the value in a textbox as an array and I need to extract from this textbox few values with respect to the index to another textbox.

eg: values in textbox1 (just an example)

120 230 420 100 70 20 89 92 48

i need even values from this.
i basically need to treat this as an array and need to get value by index.

textbox1.array.index[2] = textbox2.text

something like this...

Please help me...


///////////////////

you need to use character array or to use substring to have an array out of the string

by Yaniv on Sunday, January 18th 2009 at 04:53 PM

string NewText;
NewText = TextBox1.Text.Replace("\r\n", "<br>");
LabelTextPlaceHolder.Text = NewText;

by kjljkl on Thursday, January 29th 2009 at 12:29 AM

fdddddfddddddg
jkljklkj

by tarak shah on Wednesday, February 18th 2009 at 07:20 AM

I need to place the <br> in Label Control for new line since Environment.NewLine didn't work neither \r\n nor \\r\\n works for Label control in C# 2.0

by roy on Friday, March 27th 2009 at 12:33 AM


zzz,tt

by adrianjamv on Thursday, May 14th 2009 at 11:48 AM

Gracias

Roberto Reff

by Brett on Tuesday, September 22nd 2009 at 03:59 PM

Use regex.Split

by Bijal on Friday, September 25th 2009 at 02:29 AM

Textbox1.text Environment.newline works fine...

by cool on Sunday, November 8th 2009 at 03:25 AM

thanks

alot

by isrshn on Sunday, November 22nd 2009 at 09:16 AM

Good. Thank you.

by hsn on Tuesday, December 8th 2009 at 08:32 AM

environment.newline is great .)

by hsn on Tuesday, December 8th 2009 at 08:32 AM

environment.newline is great .)

by asas on Thursday, February 11th 2010 at 12:26 AM

<br/> sasasas <br/>

by shital on Tuesday, March 30th 2010 at 03:03 AM

Hello,

Enviornment.Newline solve my problem

Tnx 4 that.

by Connor on Thursday, April 15th 2010 at 08:59 PM

I dont have a Lines property, nor does splitting by newline do anything. Im using WPF... Help?

by Jake on Monday, April 19th 2010 at 05:18 PM

Does any of you guys know how to fill a textbox with alligned multiple columns?
Should look like this:

Lable A Lable B
Value #1 Value #2
Value #3 Value #4

Etc.
Tnx alot!

by PHB on Thursday, May 6th 2010 at 07:13 AM

Enviornment.Newline option is great. Thanks a lot for the post.

by Shariq on Thursday, June 3rd 2010 at 07:36 AM

text2.text= text1.text vbcrlf text2.text

by KC on Monday, June 14th 2010 at 02:10 AM

thx for environment.newline . nice dude

by marck_don on Sunday, June 20th 2010 at 04:11 AM

Most important topics. I need this topics. Thanks for sharing
http://www.adnpost.com

by Nick on Sunday, June 27th 2010 at 07:31 AM

I have a different question...

How can i write the character " in a textBox, or in a text file etc?????

Eg. If i want to say:
Console.WriteLine("number "1" ");
It's obvious that the " makes the compiler think that the text ends there.
But I don't have another choice, I Have to use this specific character!!!

Please help.

by nick on Sunday, June 27th 2010 at 07:42 AM

Well... I found the answer myself immediately.

Its Console.WriteLine("number \"1\" ");

So this is solved.

by Woodsman on Friday, August 20th 2010 at 04:59 PM

Check the textbox for "AcceptsReturn" property and set that to true.

by Tshepo T on Tuesday, September 14th 2010 at 06:10 AM

Hi,manish

can you please break down this below coding for me a just a beginner don't really get and i'm i need of it

//s.Length = s.Length \"\\n\".ToString ;
// }


char[] delimiter = \"\\r\\n\".ToCharArray();

// Get the lines in the text box
string[] s2 = s.Trim().Split(delimiter);

// Display the number of lines
Response.Write(s2.Length.ToString() \" lines.\");

Thanks in advance


by Tshepo T on Tuesday, September 14th 2010 at 06:10 AM

Hi,manish

can you please break down this below coding for me a just a beginner don't really get and i'm i need of it

//s.Length = s.Length \"\\n\".ToString ;
// }


char[] delimiter = \"\\r\\n\".ToCharArray();

// Get the lines in the text box
string[] s2 = s.Trim().Split(delimiter);

// Display the number of lines
Response.Write(s2.Length.ToString() \" lines.\");

Thanks in advance


by Tshepo T on Tuesday, September 14th 2010 at 06:10 AM

Hi,manish

can you please break down this below coding for me a just a beginner don't really get and i'm i need of it

//s.Length = s.Length \"\\n\".ToString ;
// }


char[] delimiter = \"\\r\\n\".ToCharArray();

// Get the lines in the text box
string[] s2 = s.Trim().Split(delimiter);

// Display the number of lines
Response.Write(s2.Length.ToString() \" lines.\");

Thanks in advance


by Nikhil Sinhal on Thursday, September 16th 2010 at 06:13 AM

txtStatus.Text = txtStatus.Text Environment.NewLine works fine!!!

by extra on Monday, November 22nd 2010 at 02:47 AM

Environment.NewLine

simply great

by tommy on Wednesday, January 5th 2011 at 05:37 AM

sorry, but this is crappy in C# / .NET

in VCL Delphi is possible to just:
Memo1.Lines.Add('Your Line 1')
Memo1.Lines.Add('Your Line 2')

Lines in multi line Textbox should support some simple Add() method! (with all \r \n thing solved AUTOMATICALLY)

by tommy on Wednesday, January 5th 2011 at 06:27 AM

ok i told this is crappy
but there is another method:

textBox1.AppendText("something \r\n");

by saeid on Monday, February 21st 2011 at 10:15 AM

Just make sure acceptreturns is set to true in textbox properties then \r works

by Mika on Monday, February 28th 2011 at 10:52 AM

Try/n/r

by Philip on Wednesday, March 16th 2011 at 06:02 PM

Environment.newline solved my problem quickly, appreciate that

by kalpa on Wednesday, May 4th 2011 at 02:47 AM

wow thanks a lot. :) Environment.Newline rocks!!! \m/

by waqar on Friday, July 8th 2011 at 02:23 PM

yes its works
'\n'
'\r'
thanx

by sag on Friday, July 22nd 2011 at 03:35 AM

OOOPs, that does work, but this also works:

The TextBox object contains an Array of strings that make up its multiple lines. To do what would normally look like just a “line feed” or carriage return you must lay it out line by line. Here is the code to do what the first tip does.

string[] newLines = new string[2];

newLines[0]="First line";
newLines[1]="Second line";

by Amit Dubey on Saturday, July 30th 2011 at 03:13 PM

Thanx It's really helpfullllllll.....

by prasad on Monday, August 29th 2011 at 12:25 AM

lkflfllflf

by prasad on Monday, August 29th 2011 at 12:27 AM

lkflfllflf

by UrsulaBauer28 on Sunday, September 4th 2011 at 05:10 AM

I took 1 st <a href="http://bestfinance-blog.com/topics/business-loans">business loans</a> when I was 32 and this aided my business very much. But, I need the credit loan once again.

by forbiddengod on Saturday, October 8th 2011 at 03:11 AM

Wer wod I see the environment.newline????pls reply asap...

by forbiddengod on Saturday, October 8th 2011 at 03:11 AM

Wer wod I see the environment.newline????pls reply asap...

by s!!!!!!!!!!!!! on Saturday, October 15th 2011 at 05:37 AM

AsASaAS

by s!!!!!!!!!!!!! on Saturday, October 15th 2011 at 05:37 AM

AsASaAS

by heijihattori[dot]com on Sunday, October 30th 2011 at 10:53 AM

Nice thank's. . . .

by sexy iranain boy on Tuesday, December 6th 2011 at 02:19 PM

Enviroment.newLinr is better

by sexy iranain boy on Tuesday, December 6th 2011 at 02:19 PM

Enviroment.newLinr is better

by sexy iranain boy on Tuesday, December 6th 2011 at 02:19 PM

Enviroment.newLinr is better

by nn on Monday, February 13th 2012 at 01:10 AM

nothing work

by nn on Monday, February 13th 2012 at 01:10 AM

nothing work

by nn on Monday, February 13th 2012 at 01:10 AM

nothing work

by Dancer on Wednesday, February 15th 2012 at 03:32 PM

Since outside sources are inconsistent I usually take the any input strings as they are, construct my own part of the string using \n as the newline char and just before using it (e.g. assigning to the Text property) - I do the following:
str.Replace("\r\n", "\n").Replace("\n", Environment.NewLine);

by www.ForeclosureHelp.org on Friday, February 24th 2012 at 04:57 PM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!

by Shyju on Monday, February 27th 2012 at 01:41 AM

Thanxxxxxxxxxxx

by AllAboutDivorce.com on Monday, March 5th 2012 at 10:30 AM

Thanks. Great blog,very useful.
<a href="http://www.allaboutdivorce.net">AllAboutDivorce.com</a>

by Sayed on Friday, March 9th 2012 at 05:47 AM

Thanks. System.Environment.NewLine, it works.

by StartCleaningService.com on Monday, March 12th 2012 at 06:27 AM


Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!
<a href="http://www.startcleaningservice.com">StartCleaningService.com</a>

by John on Thursday, March 22nd 2012 at 01:55 PM

EASY WAY - Make sure edit control is set to multiline. Right-click on it and select "Edit Text Lines"

by EducationRequirements.net on Monday, April 9th 2012 at 01:33 AM


The TextBox object contains an Array of strings that make up its multiple lines. To do what would normally look like just a <a href="http://www.educationrequirements.net">EducationRequirements.net</a>

by click here on Tuesday, April 24th 2012 at 04:44 AM

Thank you for bringing this to our attention. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.

by master in health administration online on Wednesday, April 25th 2012 at 06:40 AM

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality information.

by Mohammed on Friday, January 25th 2013 at 02:05 PM

Works like a charm, thanks

by jay on Thursday, October 31st 2013 at 05:42 PM

sir
in access memo field its store like jay 150 raj 300
but i want to store
jay 150
raj 300

pls give command how i store this way

by jay on Thursday, October 31st 2013 at 05:42 PM

sir
in access memo field its store like jay 150 raj 300
but i want to store
jay 150
raj 300

pls give command how i store this way


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources