User:Woozle/VB/Settings manager/sample code

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search

loading settings

This form loads at startup. If you use sub main() startup instead, then put this code there instead. (This code has been adapted somewhat and hasn't actually been tested in this form, but it worked in the original context.)

' common objects:
Private objPrefs As clsSettingsXML
Private xtf As xtForm
Private WithEvents ctrlList As xtListBox

Private Sub Form_Load()

' create form saver:
    Set xtf = New xtForm
' connect the saver with the actual form object:
    xtf.Init Me
      
' create listbox saver:
    Set ctrlList = New xtListBox
' associate it with the listbox control:
    ctrlList.Init Me.lbxFolders, True
    
    MakeSettings
    objPrefs.Root.Load
    End With
' destroy the Settings Manager so it won't have bad memories:
    Set objPrefs = Nothing
End Sub

saving settings

Private Sub Form_Unload(Cancel As Integer)
    Status = "Saving settings..."
    MakeSettings
    objPrefs.Save
End Sub

common code

Code used for both loading and saving

Private Sub MakeSettings()
' create the Settings Manager object:
    Set objPrefs = New clsSettingsXML
    With objPrefs
        .Init
        .Load False   ' load settings from XML file
' add the two savers to the Settings Manager tree:
        .Root.AddItem xtf
        .Root.AddItem ctrlList
    End With
End Sub