C# 字符串多行显示/文本换行以textbox为例讲解
程序员文章站
2023-12-11 23:41:28
方法1:以textbox为例
①:先设置textbox的属性multiline为true
②:组织好显示字符串:fistline(第一行要显示的字符)、secondli...
方法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)其实原理都一样,效果也差不多