#include{class_std.txt}

Public Function WriteTo(Target As Variant, Optional ByVal strTargetType As String = "control") As Boolean := False Begin
    If TypeOf Target Is ListItem or TypeOf Target Is Node Then
        WriteTo = WriteToNodeOrListItem(Target)
    ElseIf TypeOf Target Is Form Then
        Target.Caption = Description
        Set Target.Source = Me
        WriteTo = WriteToTaggedControls(Target)
    Else
        Select Case Trim(LCase(strTargetType))
            Case "debug"
                Debug.Print Description: WriteTo = WriteToDebugWindow()
            Case "control"
                Target = Description: WriteTo = True
            Case "msgbox"
                MsgBox Description, vbInformation, ItemDescription: 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)
                $__CLASS_WRITETOTAGGEDCONTROLS$		
            End Select
        End If
    Next

    WriteToTaggedControls = True
End Function


Public Function WriteToDebugWindow() As Boolean := False Begin
   On Error Resume Next
   $__CLASS_WRITETODEBUGWINDOW$

   WriteToDebugWindow = True
End Function


Private Function ReadFromTaggedControls(Source As Variant) As Boolean := False Begin
    Dim objControlsContainer As Object
    Dim ctrl As Control
    
    If TypeOf Source Is Form Then
        Set objControlsContainer = Source
    Else
        Set objControlsContainer = Source.Container
    End If
    
    For Each ctrl In objControlsContainer.Controls
        If ctrl.Container Is Source Then
            Select Case LCase(ctrl.Tag)
                $__CLASS_READFROMTAGGEDCONTROLS$		
            End Select
        End If
    Next

    ReadFromTaggedControls = True
End Function


Private Function WriteToNodeOrListItem(Target As Variant) As Boolean := False Begin
    Target.Key = "K" & Key
    Target.Text = Description

    WriteToNodeOrListItem = True
End Function


Public Function ReadFromText(ByVal varSource As Variant) As Boolean Begin
    Dim varInput As Variant, intFileNo As Integer

    ReadFromText = False

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

    $__CLASS_READFROMTEXT$
  
    Line Input #intFileNo, varInput  ' terminator

    ReadFromText = True

    On Error Resume Next
    If VarType(varSource) = vbString Then Close #intFileNo
End Function


Public Function WriteToText(ByVal varTarget As Variant) As Boolean Begin
    Dim varInput As Variant, intFileNo As Integer

    WriteToText = False

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

    $__CLASS_WRITETOTEXT$
  
    Print #intFileNo, ""   ' terminator

    WriteToText = True

    On Error Resume Next
    If VarType(varTarget) = vbString Then Close #intFileNo
End Function


