不保存用户设置(Settings.Default.Save();)
在项目的属性中,我创建了一个像这样的设置,
NumberOfUsers int User 10
列是名称、类型、范围和值。
然后有一个组合框,用户可以在其中设置“NumberOfUsers”。该组合具有 SelectedIndexChanged 事件,每当用户更改组合的值时,我都会保存更改。代码如下:
Settings.Default.NumberOfUsers = combo1.SelectedIndex;
Settings.Default.Save();
带有此组合的表单从父级调用为 frm.ShowDialog();
,在子表单的构造函数中,我尝试根据设置条目设置组合的选定索引
combo1.SelectedIndex = Settings.Default.NumberOfUsers;
但是,这不起作用,即组合不会从设置中提取值,而是默认为 0 作为选定索引。
有人知道我哪里出错了吗?
In the properties of project I created a setting like this
NumberOfUsers int User 10
The columns are Name, Type, Scope and Value.
Then there is a ComboBox where user can set a 'NumberOfUsers'. This combo has SelectedIndexChanged event where I save the changes whenever the user change the value of combo. Here's the code:
Settings.Default.NumberOfUsers = combo1.SelectedIndex;
Settings.Default.Save();
The form with this combo is called from the parent as frm.ShowDialog();
and in Constructor of the child form I try to set combo's selected index based on the Settings entry
combo1.SelectedIndex = Settings.Default.NumberOfUsers;
However, this DOES NOT work, that is the combo does not pull the value from setting, but it rather defaults to 0 as selected index.
Anyone knows where I make mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你没有犯任何错误。据我所知,VS 还会在构建过程中重新生成配置文件。我会在测试机上尝试一下。
它将用户设置存储在用户的 AppData(本地或漫游?)中,如下所示:
中间的长文件夹名称会有所不同,但我想你会很容易找到它。查看此文件,看看它是否存储了新值。
我在很多地方使用它并且在生产环境中它运行良好。
老实说,在开发机器上我从来没有让它工作过。
I think you do not make any mistake. As far as I know VS also regenarates the config file during the build. I would try it on a test machine.
It will store user settings in your user's AppData (local or roaming?) under something like this:
The long foldername in the middle will be s.g. different but I guess you will find it easily. Have a look at this file and see if it stores the new values or not.
I use it many places and in the production environment it's working well.
To be honest, on the dev machine I have never had it working.
也许是这样:
Maybe that way:
您是在组合框中输入 NumberOfUsers 的新值还是从下拉列表中选择它?
如果您输入值 SelectedIndex 将不会更改,因此不会触发任何事件。
ComboBox 是否填充了从 0 到 10 的值,或者您是否有代码来处理 ArgumentOutOfRangeExceptions
Are you typing in the new value for NumberOfUsers in the ComboBox or selecting it from the drop down list?
If you type in the value SelectedIndex will not change so no event will be fired.
Also is the ComboBox being populated with values going from 0 to 10 or do you have code to handle ArgumentOutOfRangeExceptions