Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSave and restore all form control settings in the registry
KeywordsSaveSetting, GetSetting, form settings
CategoriesTips and Tricks
 
When the form loads, loop through the Controls collection using GetSetting to load each control's saved value. This routine sets the control's default value. It also skips certain kinds of controls (Label, CommandButton, etc.) with values that the user cannot change.

Note that this method can have problems if certain controls are not loaded in the proper order. In this example, the FileListBox control is loaded before the DirListBox control. But the program needs the DirListBox to load first to give the FileListBox its directory path. This program doesn't bother to fix this.

When the program exits, it loops through the Controls collection saving the default property values for each control (skipping the same control types).

Helper function ControlName returns a control's name with index if appropriate.

 
' Load saved values.
Private Sub Form_Load()
Dim ctl As Control

    On Error Resume Next
    For Each ctl In Controls
        ' Skip certain kinds of controls.
        Select Case TypeName(ctl)
            Case "PictureBox", "Image", _
                "Line", "Shape", "Timer", _
                "CommandButton", "Label"
            Case Else
                ' Load the saved value.
                ctl = GetSetting("SaveFormSettings", _
                    "Values", ControlName(ctl), ctl)
        End Select
    Next ctl
End Sub

' Save control values.
Private Sub Form_Unload(Cancel As Integer)
Dim ctl As Control

    For Each ctl In Controls
        ' Skip certain kinds of controls.
        Select Case TypeName(ctl)
            Case "PictureBox", "Image", _
                "Line", "Shape", "Timer", _
                "CommandButton", "Label"
            Case Else
                ' Save the control's value.
                SaveSetting "SaveFormSettings", _
                    "Values", ControlName(ctl), ctl
        End Select
    Next ctl
End Sub

' Return the control's name including an index
' if appropriate.
Private Function ControlName(ByVal ctl As Control) As String
    On Error Resume Next
    ControlName = ctl.Name & "(" & ctl.Index & ")"
    If Err.Number <> 0 Then ControlName = ctl.Name
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated