Reputation: 191
I have a combo box that holds employee names that are selected by that employee. When I add a name to the combo box with a button click is there a way to keep the name in the combo box for the next time the program is started? Below is my code to add the employee names that I know need to be in the list and then the code to add a name to the combo box.
private void employeeSelect_Load(object sender, EventArgs e)
{
cboEmpName.Items.AddRange(new string[] { "John", "Roger", "Bill", "Jason", "James" });
}
private void addTechbtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtAddTech.Text))
{
cboEmpName.Items.Add(txtAddTech.Text);
txtAddTech.Clear();
MessageBox.Show("Technician has been added");
}
else
{
MessageBox.Show("Enter a name to add to the list");
}
}
Upvotes: 0
Views: 2165
Reputation: 9739
If you just want to persist the last value selected by user, UserSetting
is the best place to save such values.
Declare user setting values as -
Right click your project >> Navigate to Properties >> Settings
Say the value is EmployeeNameCombovalue, then you can read the value like this -
var empNameVal = Properties.Settings.Default.EmployeeNameCombovalue;
And to save some value in it -
Properties.Settings.Default["EmployeeNameCombovalue"] = cboEmpName.Text;
Properties.Settings.Default.Save();
So next time, when the program stars, you check if the value is there in user setting, if yes, set it to the combobox.
But, if it is not just about saving a particular value, and you want to save all the drop drop down values, then I suggest to store these values in some persistent storage like DB, and bind your combobox from that data. And whenever you add a new value to it, add the same in DB as well and update your combobox data source.
https://msdn.microsoft.com/en-us/library/x8160f6f%28v=vs.110%29.aspx
Upvotes: 1
Reputation: 16956
There are various options available.
At some point, the answer boils down to a matter of urgency. I'd say you'll end up with these options:
.NET Application settings
, provides the easiest way to access your
settings at runtime (Check this Example
).Upvotes: 0