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:
- 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:
- Textbox. 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 ";
In this way, the newline function of the C # textbox is implemented.