Any good examples of how to use Applications settings

Hi,

I'm trying to use Application settings to persist the size of a window between sessions in c# Express.
I've tried following the help files but it's all way too complex - I can work up to comlpex once I've got something working! 

Does anyone know if there is a simple 'how to' anywhere, covering the basics
What code do I need to add (to save )
What are the steps in the designer

Many Thanks for any help
Baffled


Answer this question

Any good examples of how to use Applications settings

  • HMF

    For what it's worth -- I use a StringCollection, say, Properties.Settings.Default.myStrings. But I only use it at Form1_Load() and Form1_FormClosing() time to restore and save the collection belonging to the class --
    Form1_Load:
        this.baseUrls = Properties.Settings.Default.BaseUrls;
        if (baseUrls = null) baseUrls = new StringCollection(); // just so I won't have to worry
    Form1_FormClosing:
        Properties.Settings.Default.BaseUrls = this.baseUrls.

    That is, I normally don't use any of the Settings during the life of the program, just loading and closing. ... which brings us back to the original question on the thread -- preserving form location and size.

    If you have a form without minimize or maximize buttons, binding the location and size seem to work OK. But everything goes all wonky if you maximize or minimize with bound size and location. And what happens if you close a minimized form is really ugly.

    So here's some tedious boilerplate for restoring normalized or maximized windows. It normalized the window while closing to preserve the normalized sizes. It's not elegant, but it does what I think it needs to.
    private void Form1_Load(object sender, EventArgs e)
    {
        Location = Properties.Settings.Default.Form1Location;
        Size = Properties.Settings.Default.Form1Size;
        // Open as normal or maximized
        if (Properties.Settings.Default.Form1WindowState ==
           FormWindowState.Maximized)
        {
           WindowState = FormWindowState.Maximized;
        }
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.Form1WindowState = WindowState;
        // Normalize to preserve normal size and location
        if (WindowState != FormWindowState.Normal)
        {
           WindowState = FormWindowState.Normal;
        }
        Properties.Settings.Default.Form1Location = Location;
       
    Properties.Settings.Default.Form1Size = Size;
       
    Properties.Settings.Default.Save();
    }


  • stage88

    The amount of code that you have to write depends a lot on what you are going to use the settings for. In the simplest case, you don't have to write a single line of code (well, since this is a C# forum, you probably have to write one line - you can't rely on the My Application framework to automatically save your settings, so you've got to put a: 

       Properties.Settings.Default.Save();


    at some convenient place)

    I would recommend that you start by using the settings designer to define your settings. It will automatically create an ApplicationSettingsBase derived class with a correctly typed property for each setting that you define. So, if you add two settings "MyStringUserScopedSetting" and "MyIntegerApplicationScopedSetting", where "MyStringUserScopedSetting" (I hope that you can guess the type & scope for these settings :)) you will see that you can access them from Properties.Settings.Default.MyStringUserScopedSetting and Properties.Settings.Default.MyIntegerApplicationScopedSetting respectively. Loading or these values are handled automagically. As mentioned above, you have to tell the settings class when to save it's values, though...

    The settings designer will also help you put the right magic goo in the app.config file in case you want to change the values for one or more settings after your application is deployed. If you are curious, you can take a look at the Settings.Designer.cs file to see exactly what it generates (you probably have to select "Show all files" in the solution explorer in order to see this file) Do take the little warning about "This is automatically generated code, don't change anything here" seriously, though - any changes you make in this file *will* be blown away whenever you add/remove/change a setting from the IDE.

    There are a couple of links below that may give you some more ideas on how this is done:

    http://msdn2.microsoft.com/library/c9db58th(en-us,vs.80).aspx

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnvs05/html/vbmysettings.asp

    Best regards,
    Johan Stenberg

  • bvimcp

    OK, thats ok, but where is properties.settings for a console application
  • KestralMike

    Specifically, I’ve found the settings designer quite easy to work with, except if a collection is used. I’ve attempted to use a Specialized.StringCollection  or an ArrayList collection as a setting in the Settings Designer, but when I try to set (or add in this case) a value to the collection, I get a run time error, “Object Reference Not Set to an Instance of the Object”.  < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    So for example, here are my steps to duplicate:

    1.       Create a new project

    2.       Right click in the Solution Project and choose Properties

    3.       Within the Project Properties, choose the settings node

    4.       In the settings grid add two lines

    a.       Name, type of string and application for the scope

    b.       Folders.

                                                                     i.      In the Type box, click the drop down box and choose Browse

                                                                   ii.      Find the System.Collections.ArrayList namespace and choose it

    5.       Notice when you set a value to the Name setting within code it works fine, but when you set a value to the Folders setting it gives the error, “Object Reference Not Set to an Instance of the Object”.

     

    Normally, on an error like this I would realize that I haven’t created the object yet, so I need to create a “new” object, but in this case, how do you create a new my.settings.Folders collection object Or is that even applicable to my problem

     

    Does anyone know if I have to create a new class inheriting from the ApplicationSettingBase class to be able to store collections in the My.Settings object This technique is described in the article “Using My.Settings in Visual Basic 2005” at http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnvs05/html/vbmysettings.asp.

     

    I appreciate any guidance anyone can offer. Thanks a million!

    Bob


  • David Parker

    Thanks for your help.

    On first reading it seems not to use the c# Express IDE application settings.  I know I can save values in a file and then read them back but I'd kind of hoped that the Application Settings in the IDE made things a bit less manual.

    This link kind of hints as much but it isn't exactly comprehensive for c#, i.e. what code do I have to add.

    http://whidbey.msdn.microsoft.com/library/default.asp url=/library/en-us/dv_mancli/html/53b3af80-1c02-4e35-99c6-787663148945.asp

    Cheers
    Steve 

  • n.ar

    Hi,

    I am working with Visual C# .Net 2003, but I supose that a "Session" has the same meaning at all C# versions. It is specific for Web Applications. The Sessions are copies of the same application use simultaneuosly by more users. The sessions are independently of each one. You can transfer variable between forms using the Session object. Use the Application object to handle data for all sessions.

    Have a fun!

    Valentin

    Do not hesitate to contact me!

    www.wwv-it.eu


  • Michelle Gutzait

    Here is another link for reference. The examples in this link are VB - but you should be able to convert them to C#:

    http://www.code-magazine.com/Article.aspx quickid=0607031


  • dcrandall

    The most painless and simple example of this is at: http://www.microsoft.com/uk/msdn/events/archive.aspx#section_p5

    Go to the Winforms and and Smartclients section and view the preserving application settings nugget. It's as easy as that!

  • TanveerRashid

    It's a bit old, but how about this article in the MSDN

    Persisting Application Settings in the .NET Framework
    http://msdn.microsoft.com/library/en-us/dndotnet/html/persistappsettnet.asp

  • Lin Jones

    Many thanks, I have it working now. Big Smile

    Cheers
    Steve

  • Any good examples of how to use Applications settings