Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Application Settings in VB 2005  »  Applying and Saving The Settings
 »  Home  »  Windows Development  »  Visual Basic 2005  »  Application Settings in VB 2005  »  Applying and Saving The Settings
 »  Home  »  Windows Development  »  Win Forms  »  Application Settings in VB 2005  »  Applying and Saving The Settings
Application Settings in VB 2005
by Ged Mead | Published  11/09/2006 | .NET Newbie Visual Basic 2005 Win Forms | Rating:
Applying and Saving The Settings

 Put Controls on a Form

    In Solution Explorer select the default Form1 and view it in the Design Window.   Drag controls on to the form as shown below:

   You can see the suggested names for the controls in the screenshot.   These are the names used in the code samples which follow.  I have also renamed the form to FrmSettings.

Applying Settings on StartUp

   The usual approach is to apply all saved settings when the application first runs which in this case means that the main form, FrmSettings is loaded.  

   As you will see, we need to refresh the controls from these settings at different times by using some of the various controls.   So instead of putting the code into the Form_Load event (and later having to repeat it in, for example a Button_Click event) we will simply place the code in a procedure that we can call when needed.    Here is that code:

Private Sub ApplyTheSettings()
Get the Settings and apply them:
  With My.Settings
    Me.BackColor = .FormColor
    Me.Size = .FormSize
    txtUserName.Text = .UserName
  End With
End Sub   

   Now, you can call that Sub from the Form_Load event:

Private Sub frmSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Populating The ComboBox

   The next piece of code isn't part of the Settings procedures, but is a neat way of listing all the known colors in a ComboBox.  You can just copy and paste it into the Form's Load event.  

Dim ColorChoice As String
  For Each ColorChoice In [Enum].GetNames(GetType(KnownColor))

Changing Values   

If you now run the project you will see that the TextBox has the default UserName of "Anonymous" and the ComboBox is populated with all the known colors.   The next step will be to write code that will change the backcolor of the form to any color the user decides to select from the ComboBox.   Again, this is fairly basic non-Settings related stuff:

Private Sub cboColors_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboColors.SelectedIndexChanged

  ' Apply the chosen color as the Form's BackColor
  Me.BackColor = Color.FromName(cboColors.Text)

End Sub

   Try running the project, type in a different user name and select a different color from the ComboBox.   When you select a different color, the Form's BackColor will change accordingly.   Note however that at this stage the My.Settings  are not changed.  In other words, this isn't an automatic update kind of situation.  Just to confirm that, close down the project after making those changes and then immediately run it again by pressing the F5 key.   As you will see, your changes haven't been saved.  Your user name is still Anonymous and the color is still that rather unexciting Olive Drab.

Saving Settings

   Saving your Settings is not difficult.   Each of those names (UserName, FormColor and FormSize) that you assigned at the start are saved as Properties in My.Settings.  As a result, as soon as you type:


   In the code window for the Form, Intellisense will appear and offer you a list which will include all three of those properties.


   You could offer the user the choice of when to save the some or all of the Settings, maybe by means of a Button or, as in our example, a CheckBox.   For the purposes of this article we will write code to automatically save  the UserName and FormColor Settings, but will give the user the choice of whether to save the FormSize also.

   The most logical place to put this code is when the form is about to close, which in this particular project also means as the application itself is about to close.   In VB 2005 this is the FormClosing event.   Take a look at the following code:

Private Sub frmSettings_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
   ' When the form closes save the user settings
   With My.Settings
       .FormColor = Me.BackColor

     ' If user wants to save current size:
         If Me.chkSaveSize.Checked Then .FormSize = Me.Size
     ' If there is a name in the name textbox:
         If txtUserName.Text.Length > 0 Then .UserName = txtUserName.Text
  End With
End Sub

   It saves the FormColor setting regardless.  It saves the UserName as long as there is a name in the TextBox to save (you may want to remove the "If" part of that line if you are happy for the user to save a name that is just an empty string).   Finally, if the user has checked the CheckBox to indicate that the FormSize is to be saved then this too is persisted to the Settings.

   You can create a whole range of Settings,  not limited to just one form.  You can apply, for instance, the FormColor choice to all forms in the project or just one form.  You can save different BackColors for different forms; or you can save BackColors of some forms, but not of others.  It's all about what you want to do to make your application as usable as possible.

Sponsored Links