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.

 

 

  

Sponsored Links