Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4,VB5,VB6
Expertise: beginner
May 28, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 settings

Function 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.WindowState
End 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.WindowState = GetSetting(AppName, frm.Name, "WindowState", _
End Function

' Delete form settings

Sub DeleteFormSettings(ByVal AppName As String, frm As Object)
    DeleteSetting AppName, frm.name
End Sub
Using these routines is straightforward:

Private Sub Form_Load()
    LoadFormSettings "MyApp", Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
    SaveFormSettings "MyApp", Me
End Sub
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date