如何将文本文件的特定文本加载到特定的文本框
How to load particular texts of text file to particular textboxes
我已经在 C# windows 表单应用程序中创建了一个项目。我正在使用 .Net Framework 4.0 版和 Visual studio 2010。
项目包含保存和加载文件按钮。还有一些文本框。
我创建了一个这样的文本文件
Serial Number = 1
Type Number = 500
Test Engineer = jay
Date = 03/05/2018
Time = 16:17:20 PM
Test1 = 1.00
Test2 = 1.76
.
.
.
Test18 = 4.66
加载文件按钮的代码:
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n');
tb_SerialNo.Text = lines[0];
tb_TypeNo.Text = lines[1];
tb_TestEngineer.Text = lines[2];
tb_Date.Text = lines[3];
tb_Test1.Text = lines[4];
tb_Test2.Text = lines[5];
}
}
当我 运行 上面的代码时,我在序列号中得到的值没有文本框是 Serial Number = 1
但我想要 1
在文本框中。相同的 Type Number
Tex 框 Type Number = 500
但在这里我也想要 500
输入数字文本框。
当您按新行拆分时,lines[0]
将存储 Serial Number = 1
。这里需要再拆分一次=
.
如果您尝试打印字符串数组中每个元素的值,您就会明白需要对代码进行哪些更改。
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n'); //To make your code more readable, you can use "Environment.NewLine" instead of '\n'
Console.WriteLine(lines[0]); //Here it will give "Serial Number = 1"
// you need to store 1 in tb_SerialNo.Text, so split lines[0] with =
//Have you tried with this.
string[] splitWithEqualTo = lines[0].Split('=');
tb_SerialNo.Text = splitWithEqualTo [1];
//Similar kind of logic you can apply for other text boxes.
}
}
要解决您的问题,您可以尝试以下方法
Console.WriteLine(lines[0]); // This will print "Serial Number = 1"
string[] slitLine = lines[0].Split('=');
Console.WriteLine(slitLine[0]); //This will print "Serial Number"
Console.WriteLine(slitLine[1]); //This will print 1, this is what you need to store in tb_SerialNo.Text, right?
这不是解决方案,但您会明白需要对代码进行哪些更改。
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n');
PrintText(tb_SerialNo, lines[0]);
PrintText(tb_TypeNo , lines[1]);
PrintText(tb_TestEngineer, lines[2]);
PrintText(tb_Date, lines[3]);
PrintText(tb_Test1, lines[4]);
PrintText(tb_Test2, lines[5]);
}
}
private void PrintText(TextBox control, string line)
{
var splitline = line.Split('=');
control.Text = splitline[1];
}
试试这个
您可以使用string.Split() or string.LastIndexOf()从原始字符串中提取您需要的部分。
例如:
在这里,当找到 '='
字符时,我们将字符串一分为二。额外的 space 由 Trim()
提升,用于从字符串的前导和尾部删除白色 space。
tb_Test2.Text = lines[5].Split('=').Last().Trim();
或
LastIndexOf()
查找指定的符号,从字符串末尾和 returns 它的位置开始搜索(如果找到它,否则 -1)。
Substring()
从提供的字符串生成一个新字符串,从一个位置开始并采用指定数量的字符。
这里,从LastIndexOf()
返回的索引开始,包括所有字符到字符串的末尾(如果不指定需要多少个字符,则全部使用,属于方法重载)。
tb_Date.Text = lines[3].Substring(lines[3].LastIndexOf("=") + 1).TrimStart();
在这两种情况下,原始字符串都保持不变。
您也可以从原始数组创建一个新数组,只包含所需的部分,然后为新数组赋值:
string[] lines2 = lines.Select(s => s.Split('=').Last().Trim()).ToArray();
tb_SerialNo.Text = lines2[0];
tb_TypeNo.Text = lines2[1];
//(...)
我已经在 C# windows 表单应用程序中创建了一个项目。我正在使用 .Net Framework 4.0 版和 Visual studio 2010。 项目包含保存和加载文件按钮。还有一些文本框。
我创建了一个这样的文本文件
Serial Number = 1
Type Number = 500
Test Engineer = jay
Date = 03/05/2018
Time = 16:17:20 PM
Test1 = 1.00
Test2 = 1.76
.
.
.
Test18 = 4.66
加载文件按钮的代码:
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n');
tb_SerialNo.Text = lines[0];
tb_TypeNo.Text = lines[1];
tb_TestEngineer.Text = lines[2];
tb_Date.Text = lines[3];
tb_Test1.Text = lines[4];
tb_Test2.Text = lines[5];
}
}
当我 运行 上面的代码时,我在序列号中得到的值没有文本框是 Serial Number = 1
但我想要 1
在文本框中。相同的 Type Number
Tex 框 Type Number = 500
但在这里我也想要 500
输入数字文本框。
当您按新行拆分时,lines[0]
将存储 Serial Number = 1
。这里需要再拆分一次=
.
如果您尝试打印字符串数组中每个元素的值,您就会明白需要对代码进行哪些更改。
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n'); //To make your code more readable, you can use "Environment.NewLine" instead of '\n'
Console.WriteLine(lines[0]); //Here it will give "Serial Number = 1"
// you need to store 1 in tb_SerialNo.Text, so split lines[0] with =
//Have you tried with this.
string[] splitWithEqualTo = lines[0].Split('=');
tb_SerialNo.Text = splitWithEqualTo [1];
//Similar kind of logic you can apply for other text boxes.
}
}
要解决您的问题,您可以尝试以下方法
Console.WriteLine(lines[0]); // This will print "Serial Number = 1"
string[] slitLine = lines[0].Split('=');
Console.WriteLine(slitLine[0]); //This will print "Serial Number"
Console.WriteLine(slitLine[1]); //This will print 1, this is what you need to store in tb_SerialNo.Text, right?
这不是解决方案,但您会明白需要对代码进行哪些更改。
private void btn_LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fdlg.FileName);
string[] lines = sr.ReadToEnd().Split('\n');
PrintText(tb_SerialNo, lines[0]);
PrintText(tb_TypeNo , lines[1]);
PrintText(tb_TestEngineer, lines[2]);
PrintText(tb_Date, lines[3]);
PrintText(tb_Test1, lines[4]);
PrintText(tb_Test2, lines[5]);
}
}
private void PrintText(TextBox control, string line)
{
var splitline = line.Split('=');
control.Text = splitline[1];
}
试试这个
您可以使用string.Split() or string.LastIndexOf()从原始字符串中提取您需要的部分。
例如:
在这里,当找到 '='
字符时,我们将字符串一分为二。额外的 space 由 Trim()
提升,用于从字符串的前导和尾部删除白色 space。
tb_Test2.Text = lines[5].Split('=').Last().Trim();
或
LastIndexOf()
查找指定的符号,从字符串末尾和 returns 它的位置开始搜索(如果找到它,否则 -1)。
Substring()
从提供的字符串生成一个新字符串,从一个位置开始并采用指定数量的字符。
这里,从LastIndexOf()
返回的索引开始,包括所有字符到字符串的末尾(如果不指定需要多少个字符,则全部使用,属于方法重载)。
tb_Date.Text = lines[3].Substring(lines[3].LastIndexOf("=") + 1).TrimStart();
在这两种情况下,原始字符串都保持不变。
您也可以从原始数组创建一个新数组,只包含所需的部分,然后为新数组赋值:
string[] lines2 = lines.Select(s => s.Split('=').Last().Trim()).ToArray();
tb_SerialNo.Text = lines2[0];
tb_TypeNo.Text = lines2[1];
//(...)