简体   繁体   中英

User settings are not saved (Settings.Default.Save();)

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?

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:

AppData\\[Local_or_Roaming]\\YourCompanyName\\yourprogram.exe_Url_[...]\\1.0.0.0\\user.config

The long foldername in the middle will be sg 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:

Properties.Settings.Default.NumberOfUsers = combo1.SelectedIndex;
Properties.Settings.Default.Save();
combo1.SelectedIndex = Properties.Settings.Default.NumberOfUsers;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM