[英]C# Forms App - Combo Box with an Array of strings as the Data source, how to set combo box value based on variable
[英]C# how to set a combo box value based on saved property settings using string
我是 C# 编程新手,想使用保存属性设置方法从组合框中保存最后一个选定的项目。
我可以使用以下代码保存最后选择的项目。
Properties.Settings.Default.model = modelBox.SelectedItem.ToString();
Properties.Settings.Default.Save();
但是不能使用下面的代码选择它(这是在初始化 winform 时)。
this.modelBox.SelectedText = Properties.Settings.Default.model;
任何建议都可以,提前致谢!
使用选定的索引而不是实际文本:
//Save
Properties.Settings.Default.model = modelBox.SelectedIndex;
Properties.Settings.Default.Save();
//Restore
this.modelBox.SelectedIndex = Properties.Settings.Default.model;
您的代码的问题在于SelectedText
并没有真正用于设置 Selected Item。 在您的情况下,它更像是只读属性。 设置 Selected 索引实际上会更改组合框的选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.