There are a variety of techniques that can be used to persist Application/Form settings/data across "sessions" (opening-closing-opening the Application) from "light-weight" to "heavy-weight:" I suggest you study this thread: [
^] to get general ideas of the techniques.
Note, in case you were wondering: WinForm native Controls, like ComboBox, are not directly serializable.
Then, decide which technique is best for you to use: Application Settings (user only ? application wide ?) ? Serialize a Custom Class in Binary, XML, or JSON format ?, etc.
Whichever strategy you use, in your case you are going to need to check in your code for the existence of persisted settings, and also decide when you need to refresh the current settings ... and the ComboBox content ... by reading from the Database again.
If I only needed to persist one set of values related to some specific Control (like a ComboBox's Items Collection), and I believed those settings would not need to be refreshed form the Database often, I might use Application Settings [
^].
Note that Application.Settings allows only one Type of object that will hold a Collection: the ArrayList. To use an ArrayList as an Application Setting,
Note that in the following example I've made the assumption that the ComboBoxItems Collection may be modified at run-time; so, in this case, you are not binding to the Database.
1. in the C# project title node in the Solution Explorer Window: double-click on Settings.settings: that opens the Settings Designer.
2. click on the dropdown of the Type field of a "blank" setting and select "Browse:" in the treeview of libraries select 'mscorlib: select System.Collection: select 'ArrayList
3. give your Property a Name in the 'Name field.
4. define whether your Property is user-defined or application-wide.
Note that if you make it application-wide then you cannot assign to the Property directly with = ! It will have no 'setter.
Here's an example of how an ArrayList Applicaton Setting named 'ComboBoxXItems defined as 'user in scope might be used:
private void Form1_Load(object sender, System.EventArgs e)
{
if (Settings.Default.ComboBoxXItems == null)
{
}
else
{
comboBox1.Items.Clear();
comboBox1.Items.AddRange(Settings.Default.ComboBoxXItems.ToArray());
}
}
private int count = 1;
private void button1_Click(object sender, System.EventArgs e)
{
comboBox1.Items.Add("new item " + count.ToString());
count++;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ArrayList CBxItemsToSave = new ArrayList(this.comboBox1.Items);
if (Settings.Default.ComboBoxXItems == null)
{
Settings.Default.ComboBoxXItems = new ArrayList();
}
else
{
Settings.Default.ComboBoxXItems.Clear();
}
Settings.Default.ComboBoxXItems.AddRange(CBxItemsToSave);
Settings.Default.Save();
}
Get started using one technique, or explore them all (best), and come back with specific questions, and code examples.