首页 > 解决方案 > 如何在xml列表框中为单个选定索引保存文本框中的项目并加载这些数据?C#

问题描述

我对 c# 比较陌生,我要求为列表框中的单个项目保存一组 5 文本框的数据。列表框中的项目将在运行时添加。对于列表中的每个项目框 5 个数据的集合应该被保存。如果我们再次单击列表框中的一个项目,数据应该会显示出来。这是要求。在这里我编写了不令人满意的代码。所以请帮助我简化程序。我传达的方式可能有点令人困惑,请为此道歉。**最终目标:对于列表框中的选定项目,数据应保存在 xml 中,如果我们单击数据应加载到那些文本框 **

 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;
        }
    }

标签: c#.netwinforms

解决方案


首先,给你的变量起正常的名字。这些tb, t,t1是什么意思?

假设一些关于人的信息存储在这些数组中。那我们就叫他们firstnames, lastnames, nicknames. 看起来好多了,是吗?


对于每个新文本框,我需要定义一个新对象,我该如何简化呢?

使用循环。

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();
}

据我了解,您使用文本框数组只是为了存储文本。这是绝对错误的。

只需创建字符串数组。

string[] firstnames = new string[size];
string[] lastnames = new string[size];
string[] nicknames = new string[size];

没有必要用初始值初始化它们。


为了方便使用放置在表单上的文本框,请将它们放在一个数组中。

TextBox[] textBoxes; // form field


textBoxes = this.Controls.OfType<TextBox>().ToArray();

或者直接设置它们:

textBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5 };

现在您可以循环使用此数组:

private void buttonSave_Click(object sender, EventArgs e)
{
    if (listBoxPeople.SelectedIndex == 0)
    {
        for (int i = 0; i < size; i++)
        {
            firstnames[i] = textBoxes[i].Text;
        }                
    }
    //...

注意友好的名字listBoxPeoplebuttonSave(当然,给他们正确的名字)。


让我们使用序列化来保存数据。

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);
}

文档链接:XmlSerializer


推荐阅读