C# 字串多行顯示/文本換行以textbox為例講解

來源:互聯網
上載者:User

方法1:以textbox為例

①:先設定textbox的屬性Multiline為true

②:組織好顯示字串:FistLine(第一行要顯示的字元)、SecondLine(第二行要顯示的字元)、。。。。。。。、第n行字元

③textbox.text="FistLine"+System.Environment.NewLine+"SecondLine"+System.Environment.NewLine+。。。。+"第N 行"+System.Environment.NewLine

當你看到上面代碼的時候可能會認為我們一般用的分行符號不是"\r\n"嗎

你會想到這種方式:textbox.text="FistLine"+“\r\n”+"SecondLine"+"\r\n"+。。。。+"第N 行"+"\r\n"

這種格式可能在運行時也不會出錯,但在Linux作業系統或其他系統可能就會出錯

WinForm的 textbox效果

Textbox例子1用("\r\n"):具體代碼:

複製代碼 代碼如下:/// <summary>
/// 將結果填充到會員預定情況框
/// </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("客人姓名:" + strName[intRows]);
AddMsgToTextBox("客人電話:" + strPhone[intRows]);
AddMsgToTextBox("預定房間:" + strRoom[intRows]);
AddMsgToTextBox("預約號:" + strNum[intRows]);
AddMsgToTextBox("預定時間:" + strTime[intRows]);
}
}

private int intCounts = 1;
/// <summary>
/// 顯示多行文本
/// </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>
/// 設定換行
/// </summary>
protected void CheckTextBox(int intCount1)
{
int iLines = 5 * (intCount1 + 2); //想顯示多少行。

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);
}

例子2:效果(dev 控制項(Memoedit)):

源碼:

複製代碼 代碼如下:/// <summary>
/// 將結果填充到會員預定情況框
/// </summary>
/// <param name="dt">預定會員的資訊表</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 = "客人姓名:" + tmpstrName[i] + System.Environment.NewLine;
string tmpstr2 = "客人電話:" + tmpstrPhone[i] + System.Environment.NewLine;
string tmpstr3 = "預定房間:" + tmpstrRoom[i] + System.Environment.NewLine;
string tmpstr4 = "預約號:" + tmpstrNum[i] + System.Environment.NewLine;
string tmpstr5 = "預約時間:" + tmpstrTime[i] + System.Environment.NewLine;
memGusetInfo.Text += string.Format("{0}{1}{2}{3}{4}", tmpstr1, tmpstr2, tmpstr3, tmpstr4, tmpstr5) + System.Environment.NewLine;
}
}

現在DEV控制項比較多人用:所以弄了一個DEV的例子,DEV有專門的文字框控制項(Memoedit)其實原理都一樣,效果也差不多

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.