C # wrap of textbox lines in winform

Source: Internet
Author: User
To display multiple lines of text in a textbox of a Windows form, you must set its multiline attribute to true.
Everyone knows this, but when you want Code It may be difficult to set multiple lines of text for the text attribute :)

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. The line feed that Windows can display must consist of two characters: Carriage Return & line feed, that is, "\ r \ n ". If "\ n" is not displayed as a line break in Windows, it is different from other operating systems such as Linux/Unix. Therefore, you can replace "\ n" with "\ r \ n.

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? Use environment. newline to ensure that the line feed effect can be properly displayed on various platforms. It can ensure that correct line breaks can be returned on different platforms. \ r \ n in windows and \ n in Linux (Mono. Therefore, the above Code should be written: Atextbox. Text =   " First line "   +
Environment. newline +   " Second line "   +
Environment. newline +   " Third Line " ;

In addition, you can use verbatim string literal (the string starting 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.

Environment is a static class located under the system namespace.ProgramAccess to the running environment and platform information. It provides many useful static attributes and methods:
Specific can refer to the msdn: http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/cpref/html/frlrfsystemenvironmentmemberstopic. asp

Related Article

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.