Save and reload form settings from system registry

Save and reload form settings from system registry

You can put VB’s registry functions to good use by creating a few reusable routines that save, reload, and delete size and state of a form in your program.

' Save form settingsFunction SaveFormSettings(ByVal AppName As String, frm As Object)    SaveSetting AppName, frm.Name, "Left", frm.Left    SaveSetting AppName, frm.Name, "Top", frm.Top    SaveSetting AppName, frm.Name, "Width", frm.Width    SaveSetting AppName, frm.Name, "Height", frm.Height    SaveSetting AppName, frm.Name, "WindowState", frm.WindowStateEnd Function' Restore form settings.Function LoadFormSettings(ByVal AppName As String, frm As Object)    Dim currWindowState As Integer    ' in case no value is in the registry    On Error Resume Next    ' If the form is currently maximized or minimized, temporarily    ' revert to normal state, otherwise the Move command fails.    currWindowState = frm.WindowState    If currWindowState  0 Then frm.WindowState = 0        ' Use a Move method to avoid multiple Resize and Paint events.    frm.Move GetSetting(AppName, frm.Name, "Left", frm.Left), _        GetSetting(AppName, frm.Name, "Top", frm.Top), GetSetting(AppName, _        frm.Name, "Width", frm.Width), GetSetting(AppName, frm.Name, "Height", _        frm.Height)    frm.WindowState = GetSetting(AppName, frm.Name, "WindowState", _        currWindowState)End Function' Delete form settingsSub DeleteFormSettings(ByVal AppName As String, frm As Object)    DeleteSetting AppName, frm.nameEnd Sub

Using these routines is straightforward:

Private Sub Form_Load()    LoadFormSettings "MyApp", MeEnd SubPrivate Sub Form_Unload(Cancel As Integer)    SaveFormSettings "MyApp", MeEnd Sub


Share the Post: