DouglasL

Can I bind the Items collection of a ComboBox to a User Setting, such that the list will be persisted with the settings

I see that User Settings can support collections, but I don't see how to enter one in the UI, and the special case of lists or collections isn't covered in the documentation. I want to save a "most recently used query strings" list and would prefer to use built-in functionality like the Settings feature...

Thanks!

-Doug



Re: Windows Forms General Can I bind a combobox list to a User Setting ?

Andrej Tozon

Hi.

go to yor Settings grid and create a new setting, e.g. MyComboItems. Set its type to System.Collections.Specialized.StringCollection.

You cannot really bind to the Items collection, but you can use DataSource. Put the following binding code in your form's constructor:

comboBox1.DataBindings.Add("DataSource", Properties.Settings.Default, "MyComboItems", true, DataSourceUpdateMode.Never);

To add items to your settings/combo, use something like:

StringCollection strings = comboBox1.DataSource as StringCollection;

strings.Add("My new item");

Properties.Settings.Default.Save();

Properties.Settings.Default.Reload();

Reload settings is the easiest way to make combobox show your changed list. If you don't want to reload whole settings, you'll have to force your combo to reread items from changed string collection.

Andrej