#include{collection_std.txt}

Public Function WriteTo(Target As Variant, Optional ByVal strTargetType As String = "control") As Boolean := False Begin
    If TypeOf Target Is ListView or TypeOf Target Is TreeView Or TypeOf Target Is ListBox Or TypeOf Target Is ComboBox Then
        WriteTo = WriteToListControl(Target)
    ElseIf TypeOf Target Is Form Then
        Set Target.Source = Me
        Target.Caption = Description
        WriteTo = WriteToTaggedControls(Target)
    Else
        Select Case Trim(LCase(strTargetType))
            Case "debug"
                WriteTo = WriteToDebugWindow()
            Case "control"
                Target = Description: WriteTo = True
            Case "msgbox"
                MsgBox Description, vbInformation, "Collection": WriteTo = True
            Case "text"
                WriteTo = WriteToText(Target)
            
            Case Else
                WriteTo = False
        End Select
    End If
End Function


Public Function ReadFrom(Source As Variant, Optional ByVal strSourceType As String = "control") As Boolean := False Begin
   'If TypeOf Source Is Form Then
   '    ReadFrom = ReadFromTaggedControls(Source)
   'Else
        Select Case Trim(LCase(strSourceType))
   '        Case "control"
   '            Me = Source  ' set My Default property
   '            ReadFrom = True
            Case "text"
                ReadFrom = ReadFromText(Source)
            
            Case Else
                ReadFrom = False
        End Select
   'End If
End Function             


Public Event Refresh


Private Function WriteToTaggedControls(Target As Variant) As Boolean := False Begin
    Dim objControlsContainer As Object
    Dim ctrl As Control
    
    If TypeOf Target Is Form Then
        Target.Caption = Description
        Set objControlsContainer = Target
    Else
        Set objControlsContainer = Target.Container
    End If
    
    For Each ctrl In objControlsContainer.Controls
        If ctrl.Container Is Target Then
            Select Case LCase(ctrl.Tag)	
                Case "item", "items"
                    WriteTo ctrl, "control"
                Case "count"
                    ctrl = Count
                Case "counttext"
                    ctrl = CountText
                Case "description"
                    ctrl = Description
            End Select
        End If
    Next

    WriteToTaggedControls = True
End Function



Private Function WriteToListControl(ctrl As Variant) As Boolean := False Begin
    Dim booSaveVisible As Boolean
    Dim itm As ListItem, nod As Node
    Dim ExistingItem As $__ITEMCLASS$
  ''Dim strIcon As String, strSmallIcon As String
    
    WriteToListControl = False
    
    If ctrl Is Printer Then
        ' ...
    ElseIf TypeOf ctrl Is ListView Then
        booSaveVisible = True ' ctrl.Visible
        ctrl.Visible = False
        ctrl.ListItems.Clear
        With ctrl.ColumnHeaders
            .Clear
            .Add , , "Description", ctrl.Width * 0.75 * 1.0
        ' ...
        End With
    ElseIf TypeOf ctrl Is TreeView Then
        ctrl.Nodes.Clear
    ElseIf TypeOf ctrl Is ListBox Or TypeOf ctrl Is ComboBox Then  
        ctrl.Clear
    End If

    For Each ExistingItem In mcolItems
        With ExistingItem
            If TypeOf ctrl Is ListView Then
                Set itm = ctrl.ListItems.Add(, "K" & .Key, ExistingItem.Description) ' , strIcon, strSmallIcon)
                ExistingItem.WriteTo itm
                    
            ElseIf TypeOf ctrl Is TreeView Then
               'strIcon = ""
               'If lngParentKey <> 0 Then
               '    Set nod = ctrl.Nodes.Add(ctrl.Nodes("K" & lngParentKey), _
                                              tvwChild, "K" & .Key, .Description) ' , strSmallIcon)
               '    ctrl.Nodes("K" & lngParentKey).Expanded = True
               'Else  ' pref has no parent
                    Set nod = ctrl.Nodes.Add(, , "K" & .Key, .Description) ' , strSmallIcon)
               'End If
               'ExistingItem.WriteTo nod
                    
            ElseIf TypeOf ctrl Is ListBox or TypeOf ctrl Is ComboBox Then
                ctrl.AddItem .Description
                ctrl.ItemData(ctrl.NewIndex) = .Key

            End If
        End With
    Next

    If TypeOf ctrl Is ListView Then
        ctrl.Visible = booSaveVisible
        ctrl.Sorted = True
        ctrl.SortKey = 0  ' by Description
    End If

    WriteToListControl = True
End Function


Public Function WriteToDebugWindow() As Boolean := False Begin
    Dim anItem as $__ITEMCLASS$

    Debug.Print Description

    For Each anItem in mcolItems
        Debug.Print "  " & anItem.Description
    Next

    Debug.Print   

    WriteToDebugWindow=True
End Function


Private Function ReadFromText(ByVal varSource As Variant) As Boolean Begin
    Dim intFileNo As Integer, varInput As Variant
    Dim lngCount As Long, lngIndex As Long
    Dim anItem as $__ITEMCLASS$

    ReadFromText = False

    If VarType(varSource) = vbString Then
        intFileNo = FreeFile():Open varSource For Input As #intFileNo
    Else
        intFileNo = varSource
    End If

    Set mcolItems = Nothing:Set mcolItems = New Collection

    Line Input #intFileNo, varInput:lngCount = Val(varInput)

    For lngIndex = 1 to lngCount
        Set anItem = New $__ITEMCLASS$
        If anItem.ReadFromText(intFileNo) Then
            Add anItem, "K" & anItem.Key
        Else
            Err.Raise 32767, "Failed to read " & anItem.ItemDescription & ": " & lngIndex & " of " & lngCount
        End If
    Next

    Line Input #intFileNo, varInput  ' terminator

    ReadFromText = True

    On Error Resume Next

    If VarType(varSource) = vbString Then Close #intFileNo
End Function
  

Private Function WriteToText(ByVal varTarget As Variant) As Boolean Begin
    Dim intFileNo As Integer, varInput As Variant
    Dim lngCount As Long, lngIndex As Long
    Dim anItem As $__ITEMCLASS$

    WriteToText = False

    If VarType(varTarget) = vbString Then
        intFileNo = FreeFile():Open varTarget For Output As #intFileNo
    Else
        intFileNo = varTarget
    End If
    
    Print #intFileNo, Count

    For Each anItem In mcolItems
        If Not anItem.WriteToText(intFileNo) Then
            Err.Raise 32767, "Failed to write " & anItem.ItemDescription 
        End If
    Next

    Print #intFileNo, ""  ' terminator

    WriteToText = True

    On Error Resume Next

    If VarType(varTarget) = vbString Then Close #intFileNo
End Function
  

