如何从多个文本框中打开保存在文件中的数据 [英] How do I open data saved in a file from multiple textboxes
问题描述
所以我的问题是我想要打开以前保存到多个文本框中的文件的数据,我想要打开该文件时,为了保存在文本框中的数据在我打开后向我显示相同的内容,例如在textbox1中它是书面名称,textbox2姓氏,textbox3出生日期等等。所以如果我按此顺序写我希望我显示,我的问题是当我打开它时显示如下:
textbox1: NameSurnameDateofbirth,与开放后我得到的所有文本框完全相同。
以下是saveFileDialog和OpenFileDialog的代码:
打开文件对话框
So my problem is that I want to open data that are previously saved to a file from multiple textboxes, and I want when I open that file in order the data that has been saved in textboxes to show me same thing after I open, e.g. in textbox1 it is written name, textbox2 surname, textbox3 date of birth etc. so if I write in this order I want me to show, my problem is when I open it shows like this:
textbox1: NameSurnameDateofbirth, the exact same thing to all textboxes I get this after opening.
Here are the codes for saveFileDialog and OpenFileDialog:
Open File Dialog
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
textBox1.Text = File.ReadAllText(label1.Text);
textBox2.Text = File.ReadAllText(label1.Text);
textBox3.Text = File.ReadAllText(label1.Text);
textBox4.Text = File.ReadAllText(label1.Text);
textBox5.Text = File.ReadAllText(label1.Text);
textBox6.Text = File.ReadAllText(label1.Text);
textBox7.Text = File.ReadAllText(label1.Text);
textBox8.Text = File.ReadAllText(label1.Text);
textBox9.Text = File.ReadAllText(label1.Text);
}
}
并保存文件对话框:
And Save File Dialog:
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, "");
File.AppendAllText(saveFileDialog1.FileName, textBox1.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox2.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox3.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox4.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox5.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox6.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox7.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox8.Text);
File.AppendAllText(saveFileDialog1.FileName, textBox9.Text);
}
}
推荐答案
如果我理解你的问题,请使用ReadAllLines [ ^ ]而不是ReadAllText。这将为您提供一系列字符串,您可以使用这些字符串将正确的行分配给相应的文本框。
类似于
If I understand your question correctly, use ReadAllLines[^] instead of ReadAllText. This would give you an array of strings which you can use to assign correct 'lines' to corresponding text boxes.
So something like
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string fileName;
string[] lines;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog1.FileName;
label1.Text = fileName;
lines = File.ReadAllLines(fileName);
textBox1.Text = lines[0];
textBox2.Text = lines[1];
textBox3.Text = lines[2];
textBox4.Text = lines[3];
textBox5.Text = lines[4];
textBox6.Text = lines[5];
textBox7.Text = lines[6];
textBox8.Text = lines[7];
textBox9.Text = lines[8];
}
}
这篇关于如何从多个文本框中打开保存在文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!