Imports System.Collections.Hashtable
Module modSettings
    Dim myHashes As Hashtable

    Sub BuildHashTable(ByVal myControl As Control, Optional ByVal isInit As Boolean = True)
        If isInit Then
            If myHashes Is Nothing Then myHashes = New Hashtable(myControl.Controls.Count)
            myHashes.Clear()
        End If
        Dim i%
        Dim c As New Control
        If myControl Is Nothing Then Exit Sub
        If myControl.Controls Is Nothing Then Exit Sub
        If myControl.Controls.Count <= 0 Then Exit Sub
        For i = 0 To myControl.Controls.Count - 1
            myHashes.Add(myControl.Controls(i).Name, myControl.Controls(i))
            BuildHashTable(myControl.Controls(i), False)
        Next
    End Sub

    Sub SetProperty(ByVal myObject As Object, ByVal Prop As String)
        If myObject Is Nothing Then Exit Sub
        Dim s$
        Try
            s = Prop
            If s <> "" Then
                Select Case myObject.GetType.ToString
                    Case "System.Windows.Forms.TextBox"
                        myObject.Text = s
                    Case "System.Windows.Forms.Label"
                        myObject.Text = s
                    Case "System.Windows.Forms.RadioButton"
                        If Mid(s, 1, 1) = "1" Then
                            myObject.checked = True
                        Else
                            myObject.checked = False
                        End If
                    Case "System.Windows.Forms.CheckBox"
                        If Mid(s, 1, 1) = "1" Then
                            myObject.checked = True
                        Else
                            myObject.checked = False
                        End If

                End Select
            End If
        Catch ex As System.Exception

        End Try
    End Sub

    Sub LoadControls(ByVal me1 As Control)
        Dim key$, value$
        Dim o As Control
        While Not EOF(FileNo)
            Try
                key = LInput(FileNo)
                o = myHashes.Item(key)
                value = LInput(FileNo)
                SetProperty(o, value)
            Catch ex As System.Exception
                FClose(FileNo)
                Throw New System.Exception(ex.Message)
            End Try
        End While
    End Sub

    Sub SaveControls(ByVal me1 As Control)
        Dim c As Object
        Dim i%, s$, t$
        If me1 Is Nothing Then Exit Sub
        If me1.Controls Is Nothing Then Exit Sub
        If me1.Controls.Count <= 0 Then Exit Sub
        For i = 0 To me1.Controls.Count - 1
            c = me1.Controls(i)
            Try
                Select Case c.GetType.ToString
                    Case "System.Windows.Forms.TextBox"
                        RSet(c.name, c.text)
                    Case "System.Windows.Forms.Label"
                        RSet(c.name, c.text)
                    Case "System.Windows.Forms.RadioButton"
                        If c.checked Then
                            RSet(c.name, "1")
                        Else
                            RSet(c.name, "0")
                        End If
                    Case "System.Windows.Forms.CheckBox"
                        If c.checked Then
                            RSet(c.name, "1")
                        Else
                            RSet(c.name, "0")
                        End If
                End Select
            Catch ex As System.Exception

            End Try
            SaveControls(me1.Controls(i))
        Next
    End Sub

    Public Sub SaveSettings(ByVal Cntl As Control, ByVal File$)
        FileNo = FOpen(File, OpenMode.Output)
        If FileNo = -1 Then Throw New System.Exception("File - " + File + " cannot be opened for writing!")
        BuildHashTable(Cntl)
        SaveControls(Cntl)
        FClose(FileNo)
    End Sub

    Public Sub LoadSettings(ByVal Cntl As Control, ByVal File$)
        FileNo = FOpen(File)
        If FileNo = -1 Then Throw New System.Exception("File - " + File + " cannot be opened for reading!")
        BuildHashTable(Cntl)
        LoadControls(Cntl)
        FClose(FileNo)
    End Sub

    Dim FileNo% = -1

    Private Sub RSet(ByVal Key$, ByVal Value$)
        Print(FileNo, Key & vbCrLf)
        Print(FileNo, Value & vbCrLf)
    End Sub

    Public Function FOpen(ByVal File$, Optional ByVal Mode As Microsoft.VisualBasic.OpenMode = OpenMode.Input) As Integer
        FOpen = FreeFile()
        Try
            FileOpen(FOpen, File, Mode)
        Catch ex As System.Exception
            Try
                FileClose(FOpen)
            Catch ex2 As System.Exception

            End Try
            Return -1
        End Try
    End Function

    Public Function FClose(ByVal FILE%) As Integer
        Try
            FileClose(FILE)
        Catch ex As System.Exception
            Return -1
        End Try
        Return 0
    End Function
    Public Function LInput(ByVal FILE%, Optional ByVal STR$ = "Error - Cannot read another line from file ") As String
        Try
            LInput = LineInput(FILE)

        Catch ex As System.Exception
            Throw New System.Exception(STR)
        End Try
    End Function

End Module