Click here to Skip to main content
15,881,615 members
Articles / .NET
Tip/Trick

Saving User Settings in Winform Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
19 Mar 2012CPOL2 min read 49.6K   16   2
The article displays how to save user setting or preference at run time.

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.

 

Image 1

 

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.Image 2

 

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.

Image 3

 

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

C++
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)


Written By
Wolters Kluwer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Article - Location of user.config? Pin
Garry Lowther14-Jun-20 23:49
Garry Lowther14-Jun-20 23:49 
GeneralMy vote of 5 Pin
gameengineer16-Dec-14 9:20
gameengineer16-Dec-14 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.