Reader Level:
ARTICLE

Save the application settings using VS2005 for subsequent application sessions

Posted by Bechir Bejaoui Articles | Visual C# April 21, 2008
It is practical to save some usefuls data that can be kept in a separate file when the application is out of the service.
  • 0
  • 0
  • 9327

Introduction

It is practical to save some usefuls data that can be kept in a separate file when the application is out of the service. Such data can be a connection string to a given data base that can be entered at the design time or even at the run time. In this case a user can add the connection string parameters by him self.

To know how to save application settings, follow those steps:

  • Add a new project by clicking File --> New project
  • The form showed below appears:

Untitled-1.gif

Figure 1

  • Select Windows application after expanding the tree view at left and select Windows as shown in the figure 1 above, a Form 1 appears in the editor.
  • Drag and drop a panel, Picture box and a label to the project.
  • Select Project --> Select the last menu item, for this case, WindowsApplication1 properties. Or select the project in the Solution explorer, then right click and select the properties menu item. This window will appear

Untitled-2.gif

Figure 2

  • Select the Settings tab. The above grid appears, it is composed by 4 columns, the first one is dedicated to enter the setting name, the second column helps developer to set the type of the given setting, the third column is used to precise the scope is either a the user or  the application it self. The last column is provided to enter the given setting value.
  • Add some settings on your choise. In this case those settings are added.

Untitled-3.gif

Figure 3

After adding the settings to the project designer, add them in each targeted control properties as follow:

  • Switch to the Form1 design mode and select Panel1 control. In properties grid, select the ApplicationSettings property, then select PropertyBinding, a button similar to this appears. Click it an the following from will be shown:

Untitled-4.gif

Figure 4

  • Select the back color property  and set it as the figure 5 shows:

Untitled-5.gif

Figure 5

The same is done to set the rest of the controls properties.

  • Set the image path of the Picture box control. For this case the image that will be set is C:\sheshong.bmp file.
  • Now, fire up the application and the result will be as shown bellow:

Untitled-6.gif

Figure 6

To remove application setting(s) do as follow:

  • Select Project --> Select the last menu item for e.g. for this case WindowsApplication1 properties and click it. Or select the project in the Solution explorer, then right click and select the properties menu item, this window will appear.

Untitled-7.gif

Figure 7

  • Click on the targeted setting row
  • Right click and chose Remove setting as the  figure 8 shows:

Untitled-8.gif

Figure 8

It is possible to make events occur when the application settings are changed or saved by adding event handlers to the Settings class events. I mean the Settings.SettingChangingEventHandler and the Settings.SettingSavingEventHandler. 

To do so, follow those steps:

  • Select Project --> Select the last menu item for e.g. for this case WindowsApplication1 properties and click it. Or select the project in the Solution explorer, then right click and select the properties menu. The properties window will appear
  • Select the Settings tab.
  • After adding some settings click the view code menu as bellow:

Untitled-9.gif

Figure 9

The following code source appears:

namespace FilterData.Properties {

   

    // This class allows you to handle specific events on the settings class:

    //  The SettingChanging event is raised before a setting's value is changed.

    //  The PropertyChanged event is raised after a setting's value is changed.

    //  The SettingsLoaded event is raised after the setting values are loaded.

    //  The SettingsSaving event is raised before the setting values are saved.

    internal sealed partial class Settings
  
 {

        public Settings()

        {

            // To add event handlers for saving and changing settings, uncomment the  lines below:

            // this.SettingChanging += this.SettingChangingEventHandler;

            // this.SettingsSaving += this.SettingsSavingEventHandler;

        }

       

        private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {

            // Add code to handle the SettingChangingEvent event here.

        }

       

        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {

            // Add code to handle the SettingsSaving event here.

        }

    }

}

COMMENT USING

Trending up