3 c#
假设我有一种表单,其中组合框有一些选项。现在,在第一次运行该程序时,用户从组合框中选择一个选项,并通过单击按钮或其他方式保存它。现在,如果用户终止应用程序并再次运行第二次,是否有任何方法可以检索上次保存的选择?
这意味着,如果您从组合框中选择 option1 并终止应用程序。一段时间后,您再次启动应用程序,现在您的组合框应将 option1 显示为已选中,因为在上一个会话中您选择了它。
我希望你能理解我的想法。
使用Settings
// To Load (after combo box binding / population)
private void LoadSelection()
{
int selectedIndex = 0;
if (int.TryParse(Properties.Settings.Default.comboBoxSelection, out selectedIndex))
{
cbMyComboBox.SelectedIndex = selectedIndex;
}
}
// saving on button click.
private void saveButton_Click(object sender, EventArgs e)
{
//set the new value of comboBoxSelection
Properties.Settings.Default.comboBoxSelection = cbMyComboBox.SelectedIndex;
//apply the changes to the settings file
Properties.Settings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)
请参阅此处了解更多详细信息。