|
Mit den VB-Methoden SaveSetting und GetSetting können Sie Daten in der Windows-Registrierung ablegen, die somit das Beenden Ihrer Anwendung überstehen und beim nächsten Start wieder eingelesen werden können. Für einzelne Werte ist dies recht trivial - doch wie können Sie den kompletten Inhalt einer ListBox oder einer ComboBox einschließlich Markierungszustand und ListIndex sichern und wieder einlesen?
Die folgenden Funktionen erledigen dies für Sie. Sie übergeben mindestens im ersten Parameter die betreffende ListBox bzw. ComboBox an. Die Angaben für AppName und Section sind optional - lassen Sie diese weg, werden automatisch der Anwendungsname und die Standardbegriff "ListBox" bzw. "ComboBox" verwendet. Der Schlüssel (Key) ist ebenfalls optional - fehlt er, wird der Name der ListBox bzw. der ComboBox verwendet. Es liegt an Ihnen, für entsprechend eindeutige Bezeichnungen zu sorgen, wenn mehr als eine Listbox bzw. ComboBox gesichert werden sollen. Wenn Ihnen die Standard-Gliederung zu flach ist, können Sie auch tiefer verschachtelte Section-Angaben wählen (siehe: "Verzweigte Settings" khwsettingsmorekeys.htm).
Auf einer solchen Verschachtelung beruht auch die eigentliche Speicherung der Listen-Elemente. Die Bezeichnungen von Section und Key werden nämlich nach diesem Prinzip zusammengefügt (durch einen "\" voneinander getrennt) als Section an SaveSetting übergeben - als Key dient statt dessen der einfache fortlaufende Index jedes Listenelements. Nur der aktuelle ListIndex wird als bezeichneter Key "ListIndex" angegeben.
Die Speicherung der Markierungen (Selection) der Elemente wird bei der ListBox durch Voranstellen einer 0 bzw. einer 1 vor den Text eines Elements abgelegt und beim Einlesen ausgewertet. Der ListIndex wird in jedem Fall (der Aufwand ist ja praktisch ohne Bedeutung) mit abgelegt und nur bei Bedarf eingelesen und gesetzt, wenn der optionale Parameter RestoreListIndex von LBGetSettings bzw. CBGetSettings auf True gesetzt ist (Voreinstellung = True).
Mit den beiden Prozeduren LBDeleteSettings bzw. CBDeleteSettings können Sie die Einträge aus der Windows-Registrierung entfernen.
Public Sub LBSaveSettings(ListBox As ListBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String)
Dim l As Long
With ListBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ListBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
On Error Resume Next
DeleteSetting AppName, Section
On Error GoTo 0
SaveSetting AppName, Section, "ListIndex", .ListIndex
For l = 0 To .ListCount - 1
SaveSetting AppName, Section, CStr(l), Abs(.Selected(l)) & .List(l)
Next 'l
End With
End Sub
Public Sub LBGetSettings(ListBox As ListBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String, _
Optional ByVal RestoreListIndex As Boolean = True)
Dim nItems As Variant
Dim l As Long
With ListBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ListBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
.Clear
nItems = GetAllSettings(AppName, Section)
If Not IsEmpty(nItems) Then
For l = 1 To UBound(nItems)
.AddItem Mid$(nItems(l, 1), 2)
.Selected(.NewIndex) = CBool(CInt(Left$(nItems(l, 1), 1)))
Next 'l
If RestoreListIndex Then
.ListIndex = nItems(0, 1)
End If
End If
End With
End Sub
Public Sub LBDeleteSettings(ListBox As ListBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String)
With ListBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ListBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
End With
On Error Resume Next
DeleteSetting AppName, Section
End Sub
Public Sub CBSaveSettings(ComboBox As ComboBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String)
Dim l As Long
With ComboBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ComboBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
On Error Resume Next
DeleteSetting AppName, Section
On Error GoTo 0
SaveSetting AppName, Section, "ListIndex", .ListIndex
For l = 0 To .ListCount - 1
SaveSetting AppName, Section, CStr(l), .List(l)
Next 'l
End With
End Sub
Public Sub CBGetSettings(ComboBox As ComboBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String, _
Optional ByVal RestoreListIndex As Boolean = True)
Dim nItems As Variant
Dim l As Long
With ComboBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ComboBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
.Clear
nItems = GetAllSettings(AppName, Section)
If Not IsEmpty(nItems) Then
For l = 1 To UBound(nItems)
.AddItem nItems(l, 1)
Next 'l
If RestoreListIndex Then
.ListIndex = nItems(0, 1)
End If
End If
End With
End Sub
Public Sub CBDeleteSettings(ComboBox As ComboBox, _
Optional ByVal AppName As String, Optional ByVal Section As String, _
Optional Key As String)
With ComboBox
If Len(AppName) = 0 Then
AppName = App.Title
End If
If Len(Section) = 0 Then
Section = "ComboBox"
End If
If Len(Key) Then
Section = Section & "\" & Key
Else
Section = Section & "\" & .Name
End If
End With
On Error Resume Next
DeleteSetting AppName, Section
End Sub
|