如何将单个选定索引列表框的文本框中的项目以xml格式保存并加载这些数据?c#
我对c#比较陌生,我有一个要求,即列表框中的一个项目应保存set 5文本框的数据。列表框中的项目将在运行时添加。对于列表框中的每个项目,应保存set 5数据。再次,如果单击列表框中的一个项目,应显示数据。这是要求。在这里,我的代码不令人满意。因此,请帮助我,我可以简化程序。我所传达的方式可能有点混乱,请为此道歉。 **最终目的:对于列表框中的选定项,数据应以xml格式保存,如果单击,数据应加载到这些文本框中**如何将单个选定索引列表框的文本框中的项目以xml格式保存并加载这些数据?c#,c#,.net,winforms,C#,.net,Winforms,我对c#比较陌生,我有一个要求,即列表框中的一个项目应保存set 5文本框的数据。列表框中的项目将在运行时添加。对于列表框中的每个项目,应保存set 5数据。再次,如果单击列表框中的一个项目,应显示数据。这是要求。在这里,我的代码不令人满意。因此,请帮助我,我可以简化程序。我所传达的方式可能有点混乱,请为此道歉。 **最终目的:对于列表框中的选定项,数据应以xml格式保存,如果单击,数据应加载到这些文本框中** namespace WindowsFormsApplication20 publ
namespace WindowsFormsApplication20
public partial class Form1 : Form
{
TextBox[] tb = new TextBox[5];
TextBox[] t = new TextBox[5];
TextBox[] t1 = new TextBox[5];
int[] tblist = new int[5];
public struct Tblist
{
string text;
}
public Form1()
{
InitializeComponent();
tb[0] = new TextBox();
tb[1] = new TextBox();
tb[2] = new TextBox();
tb[3] = new TextBox();
tb[4] = new TextBox();
t[0] = new TextBox();
t[1] = new TextBox();
t[2] = new TextBox();
t[3] = new TextBox();
t[4] = new TextBox();
t1[0] = new TextBox();
t1[1] = new TextBox();
t1[2] = new TextBox();
t1[3] = new TextBox();
t1[4] = new TextBox();
}
here for every new text box I need to define an new object how can i simplify this?
以下是按钮单击时的内容:它将文本框中的所有数据保存为列表框中选定的索引
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
tb[0].Text = textBox1.Text;
tb[1].Text = textBox2.Text;
tb[2].Text = textBox3.Text;
tb[3].Text = textBox4.Text;
tb[4].Text = textBox5.Text;
}
if (listBox1.SelectedIndex == 1)
{
t[0].Text = textBox1.Text;
t[1].Text = textBox2.Text;
t[2].Text = textBox3.Text;
t[3].Text = textBox4.Text;
t[4].Text = textBox5.Text;
}
if (listBox1.SelectedIndex == 2)
{
t1[0].Text = textBox1.Text;
t1[1].Text = textBox2.Text;
t1[2].Text = textBox3.Text;
t1[3].Text = textBox4.Text;
t1[4].Text = textBox5.Text;
}
}
我们如何实现foreach语句,以便每次单击并保存输入的数据
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
/* textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = " ";*/
/*TextBox[] tb = new TextBox[5];*/
textBox1.Text = tb[0].Text;
textBox2.Text = tb[1].Text;
textBox3.Text = tb[2].Text;
textBox4.Text = tb[3].Text;
textBox5.Text = tb[4].Text;
}
if (listBox1.SelectedIndex == 1)
{
/* textBox1.Text = textBox2.Text = textBox3.Text= textBox4.Text = textBox5.Text = " ";*/
/*TextBox[] t = new TextBox[5];*/
textBox1.Text = t[0].Text;
textBox2.Text = t[1].Text;
textBox3.Text = t[2].Text;
textBox4.Text = t[3].Text;
textBox5.Text = t[4].Text;
}
if (listBox1.SelectedIndex == 2)
{
/* textBox1.Text = textBox2.Text = textBox3.Text= textBox4.Text = textBox5.Text = " ";*/
/*TextBox[] t = new TextBox[5];*/
textBox1.Text = t1[0].Text;
textBox2.Text = t1[1].Text;
textBox3.Text = t1[2].Text;
textBox4.Text = t1[3].Text;
textBox5.Text = t1[4].Text;
}
}
首先,给变量起一个普通的名字。这些
tb
,t
,t1
是什么意思
假设一些关于人的信息存储在这些数组中。然后让我们叫他们名字
,姓
,昵称
。看起来好多了,嗯
对于每个新的文本框,我需要定义一个新的对象,我如何简化它 使用循环
const int size = 5; // form field
TextBox[] firstnames = new TextBox[size];
TextBox[] lastnames = new TextBox[size];
TextBox[] nicknames = new TextBox[size];
for (int i = 0; i < size; i++)
{
firstnames[i] = new TextBox();
lastnames[i] = new TextBox();
nicknames[i] = new TextBox();
}
无需使用初始值对其进行初始化
为了方便使用表单上的文本框,请将它们放置在一个数组中
TextBox[] textBoxes; // form field
textBoxes = this.Controls.OfType<TextBox>().ToArray();
现在,您可以在循环中使用此阵列:
private void buttonSave_Click(object sender, EventArgs e)
{
if (listBoxPeople.SelectedIndex == 0)
{
for (int i = 0; i < size; i++)
{
firstnames[i] = textBoxes[i].Text;
}
}
//...
非常简洁
数据加载:
using (var stream = new FileStream("data.xml", FileMode.Open))
{
firstnames = (string[])xmlSerializer.Deserialize(stream);
}
指向文档的链接:
private void buttonSave_Click(object sender, EventArgs e)
{
if (listBoxPeople.SelectedIndex == 0)
{
for (int i = 0; i < size; i++)
{
firstnames[i] = textBoxes[i].Text;
}
}
//...
XmlSerializer xmlSerializer = new XmlSerializer(typeof(string[]));
using (var stream = new FileStream("data.xml", FileMode.Create))
{
xmlSerializer.Serialize(stream, firstnames);
}
using (var stream = new FileStream("data.xml", FileMode.Open))
{
firstnames = (string[])xmlSerializer.Deserialize(stream);
}