0

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.

Customer 1

Customer 2

Customer 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();
            }
        }
    }
Community
  • 1
  • 1
muttBunch
  • 123
  • 2
  • 8
  • You could essentially have 3 arrays of strings in `Properties.Settings.Default` and set the controls' text properties to that of the `Settings` array[]. Creating array variables in Application Settings does require a little bit of _working around_. I'll attempt my best to provide an answer. – Alex Diamond Jun 19 '16 at 05:56

1 Answers1

1

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: result

Community
  • 1
  • 1
Alex Diamond
  • 586
  • 3
  • 27
  • Thank you Alex, I will give this a shot - I appreciate you taking the time to look at this. – muttBunch Jun 19 '16 at 19:43
  • @muttBunch No problem, let me know if it worked out for you or if there's something that needs clarification or explanation. – Alex Diamond Jun 20 '16 at 00:18
  • I tried playing with this for a few hours and I'm thinking I may have the "type" set wrong in my settings.settings file. I'm receiving the error: "c# 'char' does not contain a definition for 'Contains' and the best extension method overload 'Iqueryable.Contains(IQueryable, string)' requires a receiver of type 'IQueryable'" in the lines with: [i].Contains(... So I'm thinking it may be because the setting is empty or my types are incorrect? Is your Settings.Default.Extension and Settings.Default.DateFormat set to Arrays for the Type? Thanks again Alex, I appreciate it. – muttBunch Jun 20 '16 at 00:58
  • Yes, they're all arrays, also make sure you have the reference `Using System.Linq` – Alex Diamond Jun 20 '16 at 01:05
  • Alex, when you have a moment, could you paste your top code in? I'm assuming the public Form1() is where you have the items being loaded into "Settings.Default."? I blew mine up and it won't revert back. Thanks again -muttBunch – muttBunch Jun 21 '16 at 00:09
  • @muttBunch Sorry, I don't understand what you meant by "top code." I assumed you had a method of adding customers using the button with that name. What happened to your project? – Alex Diamond Jun 21 '16 at 00:19
  • Sorry Alex, I meant, in your screenshot how you cannot see the rest of the code that is up top. I goofed something in the XAML but I ended up find out what it was. Your suggestion worked perfectly and I can cycle through the first 2 in the list box and it's changing the saved .txt or .log but when I hit the 3 on down in the listbox, I get the wonderful "Additional information: Index was out of range. Must be non-negative and less than the size of the collection".So I'm thinking my array for the combo box is only set to 2 - I'm not sure whether there's a different way to approach that.Thanks – muttBunch Jun 22 '16 at 01:05
  • Check out the code block that I edited in the original post under the screenshots. Since I'm novice, do you think that top "if" statement is contributing to something? – muttBunch Jun 22 '16 at 01:11