How to store user’s preferences in a Windows Forms application

Download Source Code

In this post let’s see how to store user’s preferences in a Windows Forms application

Introduction

In this post we will see how to store user’s preferences in a Windows Forms application that persists even after closing the application. Here, we will give users an option to select font and background color for the application. Font and background color is then saved in the application settings

Description

We will use Application settings to store user’s preferences Properties.Settings class will be used to get and set application settings

Step 1:

Create a new Windows Forms Application with following controls in a form. Also drag a FontDialog and a ColorDialog control on the form

My App_2013-03-20_07-48-28

Step 2:

Open Settings.settings under Properties in Solution Explorer and add two settings, MyFont and MyBackgroundColor with default values for font and background color. These settings take four values, Name, Type, Scope and Value. Name is name of the setting, Type is the data type of configuration to store, Scope indicates setting is read-only (Application) or read-write (User) and Value is the default value for the setting

 Settings.settings

These settings with default values are stored in the App.config file under userSettings tag with following format

C#

<userSettings>
     <UserPrefs_WinForms.Properties.Settings>
          <setting name="MyFont" serializeAs="String">
               <value>Arial, 8.25pt</value>
          </setting>
          <setting name="MyBackgroundColor" serializeAs="String">
               <value>Purple</value>
          </setting>
     </UserPrefs_WinForms.Properties.Settings>
</userSettings>

VB.NET

<userSettings>
     <UserPrefs_WinFormsVB.My.MySettings>
          <setting name="MyFont" serializeAs="String">
               <value>Arial, 8.25pt</value>
          </setting>
          <setting name="MyBackgroundColor" serializeAs="String">
               <value>Purple</value>
          </setting>
     </UserPrefs_WinFormsVB.My.MySettings>
</userSettings>

But when you change these settings dynamically, these values are stored in C:Documents and Settings[USER]Local SettingsApplication Data[company][applicationname][version]

To reset these settings to default values you can use Reset method Properties.Settings.Default.Reset() in C# and My.Settings.Reset() in VB

Step 3:

Write following in the Click event of buttons

C#

private void btnChangeFont_Click(object sender, EventArgs e)
{
     if (fontDialog1.ShowDialog() == DialogResult.OK)
     {
          Font font = fontDialog1.Font;
          Properties.Settings.Default.MyFont = font;
          Properties.Settings.Default.Save();
          this.Font = Properties.Settings.Default.MyFont;
     }
}

private void btnChangeBackground_Click(object sender, EventArgs e)
{
     if (colorDialog1.ShowDialog() == DialogResult.OK)
     {
          Color backgroundColor = colorDialog1.Color;
          Properties.Settings.Default.MyBackgroundColor = backgroundColor;
          Properties.Settings.Default.Save();
          this.BackColor = Properties.Settings.Default.MyBackgroundColor;
     }
}

VB.NET

Private Sub btnChangeFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeFont.Click
     If fontDialog1.ShowDialog() = DialogResult.OK Then
          Dim font As Font = fontDialog1.Font
          My.Settings.MyFont = font
          My.Settings.Save()
          Me.Font = My.Settings.MyFont
     End If
End Sub

Private Sub btnChangeBackground_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeBackground.Click
     If colorDialog1.ShowDialog() = DialogResult.OK Then
          Dim backgroundColor As Color = colorDialog1.Color
          My.Settings.MyBackgroundColor = backgroundColor
          My.Settings.Save()
          Me.BackColor = backgroundColor
     End If
End Sub

Here, we are changing default values of MyFont and MyBackgroundColor settings with the one selected by the user. We used FontDialog and ColorDialog controls to enable users to select font and color. Settings that we entered in the Settings.settings is accessed as property in strongly typed way using Properties.Settings.Default in C# which is the static member of Properties.Settings class that returns your application’s Settings. In VB.NET, these application settings are accessed using My.Settings. Properties.Settings.Default.MyFont and Properties.Settings.Default.MyBackgroundColor are set to the font and background color selected by the user. Then Properties.Settings.Default.Save() method is called to save the settings. My.Settings.Save() is used in VB.NET

Step 4:

Write following in Load event of the form

C#

private void Form1_Load(object sender, EventArgs e)
{
     this.Font = Properties.Settings.Default.MyFont;
     this.BackColor = Properties.Settings.Default.MyBackgroundColor;
}

VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Me.Font = My.Settings.MyFont
     Me.BackColor = My.Settings.MyBackgroundColor
End Sub

In the Load event of the form, form’s Font and BackColor are set from the setting

One thought on “How to store user’s preferences in a Windows Forms application

  1. Pingback: App.Config Vb.Net Usersettings

Leave a Reply

Your email address will not be published. Required fields are marked *