0
Answer

Saving data using Properties.Settings

Rano AH

Rano AH

11y
994
1
Hi
I have two questions here related to each other, I'll try to explain the problem and make it clear, hope to get the solution as soon as possible.
I have four comboBoxes which being all filled with default format of items each time I run my application.
Suppose that I need to change the current selection of items, and therefore I selected an Item form ComboBox1, another item from ComboBox2, another item from ComboBox3 and one item from comboBox4 and wanted the save this current format using Properties.Settings.
Below is my code, Please have a look and let me know how it is possible to load my stored items automatically next time I run  my application. Saving the settings method works fine with me, but when I try to call loadStoredData method in the form load, all four comboBoxes will be empty (null values). Any idea?
Also, What condition I need to set to check if there is a saved data? and in this case the comboBoxes must filled with the saved data (maybe by calling LoadStoredData method) instead of the default format of the comboBoxes. in case of no stored data, then the  comboBoxes will display the default format (filled automatically with items using other function (fillComboBoxesByDefault).


//Declaring globaly:
string [] selectedItemsList = new string [4];
string [] MyItems = new string [4];
private string [] saveSettings()
{
String data1 = comboBox1.SelectedItem.ToString();
String data2 = comboBox2.SelectedItem.ToString();
String data3 = comboBox3.SelectedItem.ToString();
String data4 = comboBox4.SelectedItem.ToString();

SelectedItemsList[0] = Properties.Settings.Default.String1 =  data1;
SelectedItemsList[1] = Properties.Settings.Default.String2 =  data2;
SelectedItemsList[2] = Properties.Settings.Default.String3 =  data3;
SelectedItemsList[3] = Properties.Settings.Default.String4 =  data4;
Properties.Settings.Default.Save();
MyItems = selectedItemsList;
return MyItems;
}
//This function must load the stored data when I run my applicaion.
private void LoadStoredData()
{
comboBox1.SelectedItem = Properties.Settings.Default.String1;
comboBox2.SelectedItem = Properties.Settings.Default.String2;
comboBox3.SelectedItem = Properties.Settings.Default.String3;
comboBox4.SelectedItem = Properties.Settings.Default.String4;
}