C # how to insert line breaks in the textbox of multiple lines?

Source: Internet
Author: User
From: http://hi.baidu.com/zhiqingcn/blog/item/416921d1f777313c9b502788.html

To display multiple lines of text in a textbox of a Windows form, you must set its multiline attribute to true.
We all know this, but you may encounter some trouble when setting multiple lines of text for the text attribute in the Code :)

You often think of directly paying a string containing the linefeed "\ n" to the text attribute: atextbox. Text = "first line \ nsecond line \ nthird line ";

However, during actual operation, you find that the line feed is not always displayed, and the result is "first linesecond linethirdline ".

In fact, it is mainly because textbox runs on Windows. A line break that Windows can display must contain two characters: Carriage Return &
Line
Feed, which must be "\ r \ n ". If "\ n" is not displayed as a line break in Windows, it is different from other operating systems such as Linux/Unix. So above
If you replace "\ n" with "\ r \ n", you can.

In fact, the problem is still not well solved, because "\ r \ n" can meet the requirements of windows, but what if it is another platform? To ensure that the line feed effect is positive on various platforms
For more information, see environment. newline. It ensures that correct line breaks are returned on different platforms. In Windows, \ r \ n is returned.
In Linux (Mono), it should be \ n. So the above Code should be written as: atextbox. Text = "first line" +
Environment. newline + "second line" +
Environment. newline + "third line ";

In addition, you can use verbatim string literal (start with @) to enter the line break: atextbox. Text = @ "first line
Second line
Third line ";

This form looks intuitive in the code, but if the code editor runs in Windows, it is still equivalent to entering \ r \ n.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.