On Application Setings in .NET 2.0 (Visual Studio 2005 Pro, C#)

G

Guest

Hello. I apologize for asking this question as the information I need is
availible via the MSDN library, however I cannot seemt o understand what I
need to do, so I'm asking here.

I have created application settings in Visual Studio (right-click on the
project, go to the settings tab, etc.) and have associated each of these
settings with a property on a form. This seems to be working right (i.e. if I
edit the default value of a setting, the setting on the form changes too, so
something must be working correctly).

I now want to save the settings to persist so that if the user opens the
program again, the same settings they had last time in each part of the forum
are still availible. How would I go about doing this?

The "how to" article in the MSDN implies that a seperate "Settings" object
has been created and that calling Settings.Save() in my closing method should
be sufficient. However, I cannot seem to access a Settings object within the
scope of my form. Do I need to create a wrapper class that extends
ApplicationSettingsBase, and if so, how would I go about doing this (the
article on ApplicationSettingsBase ha it applying to a form, however it
manually sets all the properties itself, and I'm guessing the form designer
does that automatically.

Thanks for your help,
Robert Fraser
 
G

Guest

Hi,



Settings object for the settings file can be accessed in the following ways.

VB.Net: My.Settings
C#: [Project Namespace].Properties.Settings.Default.[SettingName]

and calling the Settings.Save method will do the trick (provided the
settings are user scoped).


Also if you are using VB.Net and
If you just want to save your setting upon shutdown there is a project level
property that does just about this.

Goto Project Properties -> Application Tab

Check the
'Save My.Settings on Shutdown'
checkbox.


Regards,
Hameer Saleem
 
D

Dave Sexton

Hi Robert,

If you open the Solution Explorer in VS 2005 there should be a special node
named, "Properties" under your project node. If you expand that node you'll
see your "Settings.settings" file.

VS 2005 adds a namespace for your Settings class since it's in the
Properties folder. You'll notice that each setting you have defined has a
Typed property defined for it in the Settings class:

// example
Color aColor = Properties.Settings.Default.AColorSetting;

(Default is created by VS so that you can access the settings defined in the
project's properties dialog at runtime. These settings will be the ones
that you are binding to in the designer and may be persisted at runtime).

Changes to Application-scoped settings are not persisted at runtime, so they
are created as read-only properties. User-scoped settings, to the contrary,
provide properties that are read/write and can be persisted. User-scoped
settings are stored in a "user.config" file located in isolated storage.

The settings returned by Properties.Settings.Default can be a mixture of
settings with different scopes, as configured in the Settings editor.

You can modify and save your current User-scoped settings like this:

Properties.Settings.Default.AColorSetting = Color.Blue;
Properties.Settings.Default.Save();

In VS 2005, you can do the following to create a DataBinding between a
control's property and a User-scoped setting's property:

1. Select a control on the Form designer
2. Open the Properties window
3. In the categorical sort mode, scroll down to "Data"
4. Expand the ApplicationSettings node
5. Select a setting from the drop-down list next to one of the properties
6. If the property you want to bind to isn't available, click the ellipses
in the "(PropertyBinding)" item and find it in the dialog that is shown

Once a data-binding has been established, you can simply call Save on the
Settings class to persist any changes to the control's property without
having to first assign the value like in my previous example. The next time
the application is started, the persisted data will automatically be loaded
and assigned to the data-bound property when the Form is constructed.

Note that VS 2005 can be misleading since it appears to add a duplexed
binding when you select an Application-scoped setting for the value of a
control's property just like it would when you select a User-scoped setting,
when in fact it's simply serializing code (into the InitializeComponent
method) that reads the value from the setting's property into the control's
property. The "binding" is not duplexed since changes to Application-scoped
settings cannot be persisted at runtime. Application-scope "bindings" exist
only at design-time. Changes to a 'bound" property will have no effect on
the Settings class.
 
G

Guest

Thank you both for your detailed, prompt, and helpful responses. Have a great
day!

Best of wishes,
Robert Fraser
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top