views:

128

answers:

3

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?

A: 

Maybe that way:

Properties.Settings.Default.NumberOfUsers = combo1.SelectedIndex;
Properties.Settings.Default.Save();
combo1.SelectedIndex = Properties.Settings.Default.NumberOfUsers;
kofucii
This is not the case as I have properties namespace in using statement
trnTash
A: 

Hi,

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 s.g. like this:

...AppData\[Local or Roaming]\YourCompanyName\yourprogram.exe_Url_24dzpy0png41rn3zbsuoyvyxh253a4n4\1.0.0.0\user.config

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.

Good luck.

-- Hudgi

Hudgi
Of course kofucii is right, you missed Properties too.
Hudgi
Did not miss it because I have it in Using statement :)
trnTash
OK, that's a possible way. :) Regarding your problem, did you check it in the AppData folder? As I mentioned user level settings are not stored in the exe.config file. Only the application level Settings are stored there.
Hudgi
A: 

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