简体   繁体   English

C#如何保存包含多个对象的设置并将其绑定到列表框中的项目

[英]C# How do you save settings containing multiple objects and bind them to items in a listbox

I wrote an XML reader that converts the XML to a flat file or removes select elements from the XML file and saves the file. 我编写了一个XML阅读器,该阅读器将XML转换为平面文件或从XML文件中删除选择元素并保存该文件。

The following controls are involved: 涉及以下控件:

listbox1 – Containing a list of customers listbox1 –包含客户列表

comboBox1 – Which contains a drop down for a selection of “.log”, “.txt”, “.XML” comboBox1 –包含用于选择“ .log”,“。txt”,“。XML”的下拉列表

txtDateFormat – Containing the date format varying by customer txtDateFormat –包含日期格式,该格式因客户而异

Using the Settings.Default…I've tested the settings for only have one customer in the listbox and that worked fine for saving the settings and reloading them when the form opens. 使用Settings.Default ...我已经测试了列表框中只有一位客户的设置,并且可以很好地保存设置并在表单打开时重新加载它们。

Now for the challenge, I've added the ability to add additional customers to the listbox1 and save those to an array. 现在面对挑战,我已经添加了将其他客户添加到listbox1并将其保存到数组的功能。 Let's say each customer in the listbox needs different settings saved. 假设列表框中的每个客户都需要保存不同的设置。 For example, 例如,

{ Customer1: “ .log”, “MM_dd_yyyy” }* {Customer1:“ .log”,“ MM_dd_yyyy”} *

{ Customer2: “ .txt”, “yyyy_MM_dd” } {Customer2:“ .txt”,“ yyyy_MM_dd”}

{ Customer3: “ .log”, No date format } {Customer3:“ .log”,无日期格式}

I'm assuming this would be controlled from listbox1_SelectedIndexChanged but how would I associate each customer to it's own independent settings for each individual customer? 我假设可以通过listbox1_SelectedIndexChanged进行控制,但是如何将每个客户与其对每个客户自己的独立设置相关联? Meaning, as I add additional customers and store the property settings, Customer1 would have different property settings then Customer2 would have different property settings and so on. 意味着,当我添加其他客户并存储属性设置时,Customer1将具有不同的属性设置,然后Customer2将具有不同的属性设置,依此类推。 So basically, when you click on each customer, you will see the settings change as well such as the dateformat, combobox, parameters, etc. I've included the screenshots of the "Options" window to hopefully make better sense. 因此,基本上,当您单击每个客户时,您也会看到设置的更改,例如日期格式,组合框,参数等。为方便起见,我添加了“选项”窗口的屏幕截图。

客户1

客户2

客户3

private void lstBoxCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstBoxCustomer);
        selectedItems = lstBoxCustomer.SelectedItems;

        if (lstBoxCustomer.SelectedIndex != -1)
        {
            for (int i = selectedItems.Count - 1; i >= 0; i--)
                lstBoxCustomer.Items.ToString();
            txtCustomerName.Text = lstBoxCustomer.SelectedItem.ToString();

            string FullCustomerName = lstBoxCustomer.SelectedItem.ToString();
            string SplitCustName = FullCustomerName.Split('(')[0];
            string SNTCodeSplit = FullCustomerName.Split('(', ')')[1];
            txtCustomerName.Text = SplitCustName;
            txtSNTCode.Text = SNTCodeSplit;
        }

        for (int i = 0; i < lstBoxCustomer.Items.Count; i++)
        {
            if (lstBoxCustomer.SelectedIndex == i)
            {
                if (Settings.Default.Setting_Default_File_Extension[i].ToString().Contains("log"))
                {
                    cmbFileExtension.SelectedIndex = 0; //Log 
                }
                else if (Settings.Default.Setting_Default_File_Extension[i].ToString().Contains("txt"))
                {
                    cmbFileExtension.SelectedIndex = 1; //Txt
                }
                txtDateFormat.Text = Settings.Default.Setting_Date_Format[i].ToString();
            }
        }
    }

You have to create 3 User Settings string arrays. 您必须创建3个用户设置字符串数组。 Use this to help you. 这个来帮助你。 The following should work out for you thereafter; 此后应为您解决以下问题;

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add(Properties.Settings.Default.Customer[0]);
        listBox1.Items.Add(Properties.Settings.Default.Customer[1]);
        listBox1.Items.Add(Properties.Settings.Default.Customer[2]);
        //I recommend using a loop to add these
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            if (listBox1.SelectedIndex == i)
            {
                if (Properties.Settings.Default.Extension[i].Contains("log"))
                {
                    comboBox1.SelectedIndex = 0; //Log 
                }
                else if (Properties.Settings.Default.Extension[i].Contains("txt"))
                {
                    comboBox1.SelectedIndex = 1; //Txt
                }
                txtDateFormat.Text = Properties.Settings.Default.DateFormat[i];
            }
        }
    }

I got a screen recording of the result: 我得到了结果的屏幕记录: 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM