473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

TextBox and New Line

How is the New Line character written to the TextBox field
(TextBox.Multiline=True)?

For example:

TextBox1.Text = "Line 1\nLine 2";

Above does not produce 2 lines in the TextBox1.

Simple question, but I am stuck on this. Thanks for your help.
Jun 21 '06 #1
6 27822
poojo,

You need to add the newline/carriage return character sequence, like so:

TextBox1.Text = "Line 1\r\nLine 2";

I prefer to use what is returned by the static NewLine property on the
Environment class (when using the string for display purposes):

TextBox1.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"poojo hackma" <poojo.com/mail> wrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...
How is the New Line character written to the TextBox field
(TextBox.Multiline=True)?

For example:

TextBox1.Text = "Line 1\nLine 2";

Above does not produce 2 lines in the TextBox1.

Simple question, but I am stuck on this. Thanks for your help.

Jun 21 '06 #2
In Windows, there should be a CR (Carrage Return) and an LF (Line
Feed). So, do the following:

TextBox1.Text = "Line 1\r\nLine 2";

or

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";
John

Jun 21 '06 #3
Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
"Q. John Chen" <qj****@email.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
In Windows, there should be a CR (Carrage Return) and an LF (Line
Feed). So, do the following:

TextBox1.Text = "Line 1\r\nLine 2";

or

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";
John

Jun 21 '06 #4
Agree.

Dave Sexton wrote:
Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
"Q. John Chen" <qj****@email.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
In Windows, there should be a CR (Carrage Return) and an LF (Line
Feed). So, do the following:

TextBox1.Text = "Line 1\r\nLine 2";

or

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";
John


Jun 21 '06 #5
You would actually both be wrong then.

The line:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Compiles to:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

There is no difference in what they actually do. The only reasons would
be for semantic/aesthetic reasons.

The last one:

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Is actually slower, since the code has to parse the string in order to
find the tokens.

Of course, this is assuming that you know what is being concatenated.
If you were iterating through a loop of items, and you didn't know what you
were concatenating, then StringBuilder is your best bet.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Q. John Chen" <qj****@email.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Agree.

Dave Sexton wrote:
Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
"Q. John Chen" <qj****@email.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
> In Windows, there should be a CR (Carrage Return) and an LF (Line
> Feed). So, do the following:
>
> TextBox1.Text = "Line 1\r\nLine 2";
>
> or
>
> TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";
>
>
> John
>

Jun 21 '06 #6
I always thought that string.Format or Concat does a better job
internally (without re-allocate the string buffer) than the '+'. Thanks
for clearing this for me.

Nicholas Paldino [.NET/C# MVP] wrote:
You would actually both be wrong then.

The line:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Compiles to:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

There is no difference in what they actually do. The only reasons would
be for semantic/aesthetic reasons.

The last one:

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);

Is actually slower, since the code has to parse the string in order to
find the tokens.

Of course, this is assuming that you know what is being concatenated.
If you were iterating through a loop of items, and you didn't know what you
were concatenating, then StringBuilder is your best bet.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Q. John Chen" <qj****@email.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Agree.

Dave Sexton wrote:
Just FYI,

Don't do:

TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";

Do:

TextBox1.Text = string.Concat("Line 1", Environment.NewLine, "Line 2");

or

TextBox.Text = string.Format("Line 1{0}Line 2", Environment.NewLine);
"Q. John Chen" <qj****@email.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
> In Windows, there should be a CR (Carrage Return) and an LF (Line
> Feed). So, do the following:
>
> TextBox1.Text = "Line 1\r\nLine 2";
>
> or
>
> TextBox1.Text = "Line1" + System.Environment.NewLine + "Line2";
>
>
> John
>


Jun 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason | last post by:
I've got this javascript routine (i found on google - thank you) in an asp.net page that on page reload sets the cursor of a textbox to the last line. It works great! Using a similar concept, I...
3
by: supster | last post by:
I'de like to change only a specific line of a textbox. Right now I am doing: string texta = mainTextBox.Text.Split( "\n".ToCharArray() ); for ( int i = 0; i <= texta.Length-1; i++ ) {
6
by: Maziar Aflatoun | last post by:
Hi, I have a little application that reads a text file line-by-line and processes each line depending on the CVS values. Now I want to change my program to capture this from a textbox. How do...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
3
by: Brad Rogers | last post by:
All, Being immersed in vb.net and trying CSharp after almost a year I forgot the differences. I like vb fixing the uppercase/lowercase names and seeming to be more flexible to code entry. ...
0
by: deja | last post by:
i've got a multiline textbox into which I have put string data from my sqlserver database via a strongly typed dataset (i've tried varchar and nvarchar to see if there was any difference - there...
0
by: michelle | last post by:
Hello VB users, Our textbox in vb net is filled with lines, every line is delimitted by a CRT. Is it possible to read the listbox line for line? Does someone has script? We don't want to use a...
2
by: mike | last post by:
how do I parse a textbox of text separated by carriage returns? I have a web form with a textbox, when the user presses submit I would like to process the text in the textbox line by line. How...
3
by: =?Utf-8?B?YWVzcGVy?= | last post by:
I have a windows forms application with a TextBox display. I use the following code to display a line in the TextBox: ...... statusTextBox.AppendText(message + "\n"); ....... I just want the...
7
by: ravenfrost | last post by:
Hello, I have a text box that I am entering items into line by line. I want to read in the data one line at a time into a variable through a loop. I know the textbox does not have a readline...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.