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 arrays of settings in the registry in two ways
KeywordsSaveSetting, GetSetting, setting, array, registry
CategoriesTips and Tricks
 
This program demonstrates two methods. First, it loops through the items in the array and saves each value separately using SaveSetting. It names the values Array1(0), Array1(1), and so forth.

The second method saves the values in a single null-delimited string: "value1value2...". You could use a comma, semi-colon, or some other printable character as the delimiter, but then the values could not contain the delimiter character.

 
Private Sub Form_Load()
Dim i As Integer
Dim values As Variant

    ' Load array 1.
    For i = txtArray1.LBound To txtArray1.uBound
        ' Each setting is saved as a separate
        ' entry with names like Array1(0),
        ' Array1(1), etc.
        txtArray1(i).Text = GetSetting( _
            "SaveArrays", _
            "Values", _
            "Array1(" & Format$(i) & ")", _
            "***")
    Next i

    ' Load array 2.
    ' All values are saved in one comma-delimited
    ' string. Note that this means the values
    ' cannot contain a comma.
    ' Get the values and split them into an array.
    values = Split(GetSetting( _
        "SaveArrays", _
        "Values", _
        "Array2", _
        "***,***,***,***,***"), vbNull)

    ' Copy the values into the controls.
    For i = txtArray2.LBound To txtArray2.uBound
        txtArray2(i).Text = values(i)
    Next i
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim values As String

    ' Save array 1.
    For i = txtArray1.LBound To txtArray1.uBound
        ' Each setting is saved as a separate
        ' entry with names like Array1(0),
        ' Array1(1), etc.
        SaveSetting _
            "SaveArrays", _
            "Values", _
            "Array1(" & Format$(i) & ")", _
            txtArray1(i).Text
    Next i

    ' Save array 2.
    ' All values are saved in one comma-delimited
    ' string. Note that this means the values
    ' cannot contain a comma.
    ' Compose the value string.
    For i = txtArray2.LBound To txtArray2.uBound
        values = values & vbNull & txtArray2(i)
    Next i

    ' Remove the leading Null.
    values = Mid$(values, 2)

    ' Save the value string.
    SaveSetting _
        "SaveArrays", _
        "Values", _
        "Array2", _
        values
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated