C # multi-line string display/text line feed using textbox as an Example

Source: Internet
Author: User

Method 1: textbox is used as an example.

①: Set the textbox attribute Multiline to true first.

②: Organize the display string: FistLine (the character to be displayed in the first line), SecondLine (the character to be displayed in the second line ),....... , Line n characters

③ Textbox. text = "FistLine" + System. Environment. NewLine + "SecondLine" + System. Environment. NewLine + .... + "Row N" + System. Environment. NewLine

When you see the above Code, you may think that the line break we generally use is not "\ r \ n "?

You will think of this method: textbox. text = "FistLine" + "\ r \ n" + "SecondLine" + "\ r \ n" + .... + "Row N" + "\ r \ n"

This format may not cause errors during running, but may occur in Linux or other systems.

Textbox Effect of WinForm:

Textbox Example 1 ("\ r \ n"): code:

Copy codeThe Code is as follows: // <summary>
/// Fill the result in the member reservation box
/// </Summary>
/// <Param name = "dt"> </param>
Private void BindGuestOrder (DataTable dt)
{
Int intRowsCount;
IntRowsCount = dt. Rows. Count;
String [] strName = new string [intRowsCount];
String [] strPhone = new string [intRowsCount];
String [] strRoom = new string [intRowsCount];
String [] strNum = new string [intRowsCount];
String [] strTime = new string [intRowsCount];
For (int intRows = 0; intRows <intRowsCount; intRows ++)
{
StrName [intRows] = dt. Rows [intRows] ["GuestName"]. ToString ();
StrPhone [intRows] = dt. Rows [intRows] ["LinkPhone"]. ToString ();
StrRoom [intRows] = dt. Rows [intRows] ["RoomName"]. ToString ();
StrNum [intRows] = dt. Rows [intRows] ["BookNo"]. ToString ();
StrTime [intRows] = dt. Rows [intRows] ["DineTime"]. ToString ();
AddMsgToTextBox ("guest name:" + strName [intRows]);
AddMsgToTextBox ("guest phone:" + strPhone [intRows]);
AddMsgToTextBox ("booking room:" + strRoom [intRows]);
AddMsgToTextBox ("reservation number:" + strNum [intRows]);
AddMsgToTextBox ("scheduled time:" + strTime [intRows]);
}
}

Private int intCounts = 1;
/// <Summary>
/// Display multiple lines of text
/// </Summary>
/// <Param name = "s"> </param>
Public void AddMsgToTextBox (string s)
{
Int intCount1;
IntCount1 = intCounts/5;
CheckTextBox (intCount1 );
TxtVIPAdvanceOrder. Text + = "\ r \ n" + s;
If (intCounts % 5 = 0)
{
TxtVIPAdvanceOrder. Text + = "\ r \ n ";
}
IntCounts ++;
}

/// <Summary>
/// Set the line feed
/// </Summary>
Protected void CheckTextBox (int intCount1)
{
Int iLines = 5*(intCount1 + 2); // The number of rows to display.

String stxt = txtVIPAdvanceOrder. Text;
String [] s = stxt. Split ('\ n ');
If (s. Length <iLines)
Return;
TxtVIPAdvanceOrder. Text = "";
For (int I = 1; I <s. Length; I ++)
{
TxtVIPAdvanceOrder. Text + = s [I] + "\ r \ n ";
}
Stxt = txtVIPAdvanceOrder. Text;
If (stxt! = "")
TxtVIPAdvanceOrder. Text = stxt. Substring (0, stxt. Length-1 );
}

Example 2: Effect (dev control (Memoedit )):

Source code:

Copy codeThe Code is as follows: // <summary>
/// Fill the result in the member reservation box
/// </Summary>
/// <Param name = "dt"> reserved member information table </param>
Private void BindGuestOrder (DataTable vardt)
{
MemGusetInfo. Text = "";
Int tmpRowsCount;
TmpRowsCount = vardt. Rows. Count;
String [] tmpstrName = new string [tmpRowsCount];
String [] tmpstrPhone = new string [tmpRowsCount];
String [] tmpstrRoom = new string [tmpRowsCount];
String [] tmpstrNum = new string [tmpRowsCount];
String [] tmpstrTime = new string [tmpRowsCount];
For (int I = 0; I <tmpRowsCount; I ++)
{
TmpstrName [I] = vardt. Rows [I] ["GuestName"]. ToString ();
TmpstrPhone [I] = vardt. Rows [I] ["LinkPhone"]. ToString ();
TmpstrRoom [I] = vardt. Rows [I] ["RoomName"]. ToString ();
TmpstrNum [I] = vardt. Rows [I] ["BookNo"]. ToString ();
TmpstrTime [I] = vardt. Rows [I] ["DineTime"]. ToString ();
String tmpstr1 = "guest name:" + tmpstrName [I] + System. Environment. NewLine;
String tmpstr2 = "guest phone:" + tmpstrPhone [I] + System. Environment. NewLine;
String tmpstr3 = "reserved room:" + tmpstrRoom [I] + System. Environment. NewLine;
String tmpstr4 = "reservation number:" + tmpstrNum [I] + System. Environment. NewLine;
String tmpstr5 = "reservation time:" + tmpstrTime [I] + System. Environment. NewLine;
MemGusetInfo. text + = string. format ("{0} {1} {2} {3} {4}", tmpstr1, tmpstr2, tmpstr3, tmpstr4, tmpstr5) + System. environment. newLine;
}
}

Now DEV controls are widely used by many people: So I got a DEV example. DEV has a special text box control (Memoedit), which works in the same way and achieves similar results.

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.