Insert line breaks into the Textbox Control

Source: Internet
Author: User
To enable a Windows Form C # textbox to display multiple lines of text with a line break, set its multiline attribute to true. Everyone knows this, but when you want Code When setting multi-line text for the text attribute, you may encounter some trouble :) you often think of directly paying a string containing the line break "/n" to the text attribute:

 
  
  
  1. 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 ". C # textbox line feed Principle In fact, it is mainly because C # textbox runs on Windows. The line feed that Windows can display must consist of two characters: Carriage Return & line feed, that is, it must be "/R/N ". If "/N" is not displayed 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. In Windows, it is/R/N, and in Linux (Mono) It should be/n. Therefore, the above Code should be written:
 
  
  
  1. Textbox. Text ="First line"+
  2. Environment. newline +"Second line"+
  3. Environment. newline +"Third line";
In addition, you can use verbatim string literal (the string starting with @) to enter the line break:
  
  
  1. Atextbox. Text = @ "first line
  2. Second line
  3. Third line ";
In this way, the newline function of the C # textbox is implemented.

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.