C# 字符串多行显示/文本换行以textbox为例讲解

  ///

  /// 将结果填充到会员预定情况框

  ///

  ///

  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;

  ///

  /// 显示多行文本

  ///

  ///

  public void AddMsgToTextBox(string s)

  {

  int intCount1;

  intCount1 = intCounts / 5;

  CheckTextBox(intCount1);

  txtVIPAdvanceOrder.Text += "

  " + s;

  if (intCounts % 5 == 0)

  {

  txtVIPAdvanceOrder.Text += "

  ";

  }

  intCounts++;

  }

  ///

  /// 设置换行

  ///

  protected void CheckTextBox(int intCount1)

  {

  int iLines = 5 * (intCount1 + 2); //想显示多少行。

  string stxt = txtVIPAdvanceOrder.Text;

  string[] s = stxt.Split('

  ');

  if (s.Length < iLines)

  return;

  txtVIPAdvanceOrder.Text = "";

  for (int i = 1; i < s.Length; i++)

  {

  txtVIPAdvanceOrder.Text += s[i] + "

  ";

  }

  stxt = txtVIPAdvanceOrder.Text;

  if (stxt != "")

  txtVIPAdvanceOrder.Text = stxt.Substring(0, stxt.Length - 1);

  }