1. To enable a textbox of Windows form to display multiple lines of text, 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 running, you find that the line feed is never changed, 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 ";
2. How can I use a scroll bar in a multi-line textbox to automatically scroll to the last line after adding text?
The scrolltocaret method is used to automatically scroll to the position of the insert operator, for example:
Textbox. appendtext (Label); // append the text and place the cursor in the insert position.
Textbox. scrolltocaret ();
This. textbox. Focus (); // get the focus
This. textbox. Select (this. textbox. textlength, 0); // The cursor is positioned at the end of the text
This. textbox. scrolltocaret (); // scroll to the cursor