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();
}
}
}
Colourised in 29ms
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.