Article Options
Premium Sponsor
Premium Sponsor

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

Reload

   The Reload method can be useful, particularly in situations where the user may want to test out various settings, colors, sizes, etc before deciding which one they most like.   What happens when this method is called is that the most recently saved version of the Settings are applied.  In the case of our example project, this will be the settings that were saved when the application was last closed down.  

   The following code in the Click event of the Reload Button will replace any unsaved changes with the version that was last saved:

Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReload.Click
   ' This uses the most recent set of user saved Settings
     My.Settings.Reload()
   ' NB. You still need to refresh the controls by applying the values
   ' as you did on Form Load
   ApplyTheSettings()
End Sub

   As you can see from the comments in the code snippet above, you still need to implement the code which applies the Settings.   That is, the Reload method itself will reload the saved values into memory but they are not automatically applied to - in our example - the BackColor, the Size and the UserName.  

  This is actually often quite a good thing because there may well be times when you only want to reload some of the saved Settings but not others.   There are various solutions for this, the most obvious one being that you create an alternative version of ApplyTheSettings (maybe called ApplySomeSettings) and only assign the reloaded values to those controls that you want changed.   I'm sure you will also have realised that you can use an approach of:

  • Save those Settings that you don't want Reloaded to a temporary variable.
  • Apply the Reload Method
  • Undo the effect of the Reload by replacing chosen Setting with the value of the temp variable.

which will work just as well and may cause less confusion than having several variations of the Apply***Settings procedures around the project.

Reset Settings

   Not to be confused with Reload, the Reset method reaches right back to those initial settings that you applied right at the start of the project's life.   It will load them into memory and the code below will apply them all.   The discussion above about how to apply the new values to only some Settings is also relevant to the Reset method and the same workarounds can be used.

   The code for the Reset button is very similar to that for Reload:

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
  
' Restore the original settings and apply them
   My.Settings.Reset()
   ApplyTheSettings()

End Sub

  Note however that if you implement the Reset method, the original values will automatically be saved as if these were the user's last saved values.   In other words, previously user saved values will be overwritten by these reset initial values    If you are not quite sure what I mean by that, try running the demo project a few times, then use the Reload button, followed by the Reset button, finally followed by the Reload button once again.   You will then see what I mean.

 

 

  

Comments    Submit Comment

Comment #1  (Posted by TobyT on 11/09/2006)
Rating
Good for beginners.
 
Comment #2  (Posted by an unknown user on 12/05/2006)
Rating
Usefull feature i've never heard about before, nice article.
 
Comment #3  (Posted by an unknown user on 12/16/2006)
Rating
It is good for beginners & and to those that are not familiar with VB 2005.
 
Comment #4  (Posted by Michael Ifland on 02/11/2007)
Rating
Thank You.
 
Comment #5  (Posted by Majed on 02/21/2007)
Rating
Still too many details are missing.
 
Comment #6  (Posted by an unknown user on 08/18/2007)
Rating
Ged's articles just keep getting better and better. Being a complete newbie I really appreciate the in depth explanation of details that I am sure are common knowledge to others.
 
Comment #7  (Posted by an unknown user on 10/19/2007)
Rating
Good beginner article
 
Comment #8  (Posted by Noname on 01/04/2008)
Rating
One big mistake:

In page 2, the formsize is set without ";" (semicolon) between size values which gives error. Developers must add ; to overcome error.
 
Comment #9  (Posted by Ged Mead on 01/05/2008)
Rating
Hi "NoName". Thanks for pointing out this problem. I suspect that in truth, this may not be so much a big mistake as a fairly common locaization/internationalisation issue which occurs in many prjects where dates, times and numbers etc are used. This is because in many Western cultures, such as here in UK, we use commas, whereas elsewhere semicolons are used as delimiters.
All I can say for sure is that the sample works perfectly on my English UK system. I'm sorry if it has caused you problems.
 
Comment #10  (Posted by i.write.code on 01/22/2008)
Rating
Perfect! Thanks for pointing out the Save and Reset methods. I was pulling out my hair over settings that always seemed to reset on their own when my application was restarted.
 
Comment #11  (Posted by Rob 73 on 05/02/2008)
Rating
Just want to say thank your for a very clear, easy to follow and extremely useful article. I was initially reluctant to try using the 'my.settings' as i 'thought' it was difficult and complex when saving user preferences, and as a result nearly tore all my hair out trying to read and write from/to text files.
Thanks from me and my 'Not so bald head'
Keep up the good work!


 
Sponsored Links