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

方法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)其实原理都一样,效果也差不多

若文章对您有帮助,帮忙点个赞!

0
2
发布时间 2013-02-02 16:18:02
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)