简体   繁体   中英

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.

The following controls are involved:

listbox1 – Containing a list of customers

comboBox1 – Which contains a drop down for a selection of “.log”, “.txt”, “.XML”

txtDateFormat – Containing the date format varying by customer

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.

Now for the challenge, I've added the ability to add additional customers to the listbox1 and save those to an array. Let's say each customer in the listbox needs different settings saved. For example,

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

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

{ Customer3: “ .log”, No date format }

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? 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. 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. 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: 结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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