Boozzz
Boozzz

Reputation: 265

C#: Saving and Loading a list of ComboBox Items to/from the app.config file

I am trying to save and load a list of combobox items to the .NET settings file (app.config).

With the following code, I want to load and save the data stored in an ArrayList cboCollection.

private void Form1_Load(object sender, EventArgs e)
{
    if (Settings.Default.cboCollection != null)
        this.comboBox1.Items.AddRange(Settings.Default.cboCollection.ToArray());
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    ArrayList arraylist = new ArrayList(this.comboBox1.Items);
    Settings.Default.cboCollection = arraylist;
    Settings.Default.Save();
}

When I open the project’s Properties pages, and select the Settings tab, I would like to store {"myItem1","myItem2","myItem3"} in an ArrayList cboCollection. Unfortunately, there is no such type System.Collections.ArrayList to select. What did I do wrong?

Upvotes: 0

Views: 3417

Answers (1)

Oğuz Sezer
Oğuz Sezer

Reputation: 320

First of all, you didn't do anything. You took this code from here.

It doesn't work, because you didn't implement cboCollection object.

First you need a setting enter image description here

then implement your class

[Serializable]
public MyClass
{
//something...
}

EDIT: OK, forget about the class and just use System.Collections.Specialized.StringCollection. This way you can add items to the settings.

System.Collections.Specialized.StringCollection itemList = new System.Collections.Specialized.StringCollection();

fill your combobox items into itemList by looping each of them.

and then you can do as follows:

Settings.Default.cboCollection = itemList;

Upvotes: 1

Related Questions