Click here to Skip to main content
Click here to Skip to main content

Saving User Settings in Winform Application

By , 19 Mar 2012
Rate this:
Please Sign up or sign in to vote.

Introduction

This articles shows how to save the user preference using Settings.setting file so that it can persist between different application sessions in Winform application.

Background

We can store these information in registry , but very few of us know about Settings.setting file which help in doing exactly this.

Using the code  

Consider this tool which asks user to enter Database connection information and provides some output based on input given by ther user from the database.

The problem here is whenever user closes the application and opens it he need to provide the same connection information again. He would definately like to have this information to be remembered so that next time he opens it he doesn't need to input it again, similar to web forms where these information are stored in user cookie.

 

 

In big application with proper user log in , these information can be stored in user preference table but for standalone tool like above its best to have it stored in the exe itself without using any file or database.

To add Settings file , select "Add new File" and go to general and add Settings.setting file like below.

 

Open the Settings.setting designer and add the setting properties name like below."Name" represents the Configurable Property name , "Type" represent type of property, "Scope" represent whether its scope is "Application" or "User". Application Scope makes the the corresponding Setting property as read only.

 

Once the configurable properties are added and saved in designer it can be used like shown in below code.

namespace Settings
{
    public partial class UserSettingsDemo : Form
    {
        string ConnectionString = string.Empty;
        public UserSettingsDemo()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtOuput.Text = "Ouput goes here...................";
        }

        private void chkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIntegratedSecurity.Checked)
            {
                txtUserId.Enabled = false;
                txtPassword.Enabled = false;
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if(TestConnection())
            txtInput.Enabled = true;
        }

        private bool TestConnection()
        {
            return true;
        }

        private void UserSettingsDemo_Load(object sender, EventArgs e)
        {
            txtServer.Text = Settings.Default.ServerNameSetting;
            txtDatabase.Text = Settings.Default.DBNameSetting;
            txtPassword.Text = Settings.Default.PasswordSetting;
            txtUserId.Text = Settings.Default.UserIdSetting;
        }
        
        private void UserSettingsDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.DBNameSetting = txtDatabase.Text;
            Settings.Default.UserIdSetting = txtUserId.Text;
            Settings.Default.PasswordSetting = txtPassword.Text;
            Settings.Default.ServerNameSetting = txtServer.Text;
            Settings.Default.Save();

        }

     

    }
}
 

On Form_Closing event the settings are assigned and saved from the corresponding textboxes.

On Form_Load these values are retrieved and assigned to the corresponding textboxes thus persisting the user information.

Points of Interest

You can also save the size of the form , color or any other user preferences using the similar approach. Persisiting the value using above method is very easy and very handy in making your application user friendly.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rishikesh_Singh
Software Developer NCS,Singapore
Singapore Singapore
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 19 Mar 2012
Article Copyright 2012 by Rishikesh_Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid