方法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"):具体代码:
复制代码代码如下:
///
/// 将结果填充到会员预定情况框
///
///
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 += "\r\n" + s;
if (intCounts % 5 == 0)
{
txtVIPAdvanceOrder.Text += "\r\n";
}
intCounts++;
}
///
/// 设置换行
///
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)):
请点击输入图片描述
源码:
复制代码代码如下:
///
/// 将结果填充到会员预定情况框
///
/// 预定会员的信息表
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;
}
}
不循环那就连续写读取行吧
string[] line2 = sr.ReadLine().Split(' '); //读取当行数据,并以空格分离赋
c = double.Parse(line2[2]);
d= double.Parse(line2[3]);
string[] line3 = sr.ReadLine().Split(' '); //读取当行数据,并以空格分离赋
e = double.Parse(line3[2]);
f= double.Parse(line3[3]);
知道一定有这么多行可以像上面那样写 不知道的话加个临时变量 判断是否读完
string str= sr.ReadLine();
if(str==null) return;//读取完毕
string[] line2 =str.Split(' '); //读取当行数据,并以空格分离赋
c = double.Parse(line2[2]);
d= double.Parse(line2[3]);
str= sr.ReadLine();
if(str==null) return;//读取完毕
string[] line3 = str.Split(' '); //读取当行数据,并以空格分离赋
e = double.Parse(line3[2]);
f= double.Parse(line3[3]);