Imports System.IO
Public Class CGirdInfo
    Public Shared LoadBusy As Boolean = False

    Public Shared Function SaveGirdInfo(ByVal gridName As String, ByRef grid As FlexCell.Grid) As Boolean
        If LoadBusy = True Then Return False

        Dim strPath As String = Application.StartupPath & "\GRID"
        If System.IO.Directory.Exists(strPath) = False Then
            System.IO.Directory.CreateDirectory(strPath)
        End If

        If System.IO.Directory.Exists(strPath) = True Then
            strPath &= "\" & gridName & ".ini"

            Dim fs As New StreamWriter(strPath, False, System.Text.Encoding.Default)
            '保存列宽
            For col As Integer = 0 To grid.Cols - 1
                fs.WriteLine("C" & col & "=" & grid.Column(col).Width)
            Next
            'For row As Integer = 0 To grid.Rows - 1
            '    fs.Write("R" & row & "=" & grid.Row(row).Height)
            'Next
            fs.Close()
        End If

        Return True
    End Function

    Public Shared Function LoadGridInfo(ByVal gridName As String, ByRef grid As FlexCell.Grid) As Boolean
        LoadBusy = True

        Dim strPath As String = Application.StartupPath & "\GRID\" & gridName & ".ini"
        If System.IO.File.Exists(strPath) = True Then
            Dim fs As New StreamReader(strPath, System.Text.Encoding.Default)
            Dim strLine As String = ""
            Dim strSplit() As String
            Dim colNum As Integer = 0
            Dim widthNum As Integer = 0

            While fs.EndOfStream = False
                strLine = fs.ReadLine()
                If Not strLine Is Nothing AndAlso strLine.Length > 0 Then
                    strSplit = Split(strLine, "=")
                    If strSplit.Length >= 2 Then
                        Try
                            colNum = Strings.Mid(strSplit(0), 2)
                            widthNum = strSplit(1)
                            grid.Column(colNum).Width = widthNum
                        Catch ex As Exception
                        End Try
                    End If
                End If
            End While

            fs.Close()
        End If

        LoadBusy = False
        Return True
    End Function

    Public Shared Function SaveRecentList(ByVal saveName As String, ByRef RecentList As System.Windows.Forms.AutoCompleteStringCollection) As Boolean
        Dim strPath As String = Application.StartupPath & "\GRID"
        If System.IO.Directory.Exists(strPath) = False Then
            System.IO.Directory.CreateDirectory(strPath)
        End If

        If System.IO.Directory.Exists(strPath) = True Then
            strPath &= "\" & saveName & ".ini"

            Dim fs As New StreamWriter(strPath, False, System.Text.Encoding.Default)
            For Each pItem As String In RecentList
                fs.WriteLine(pItem)
            Next
            fs.Close()
        End If

        Return True
    End Function

    Public Shared Function LoadRecentList(ByVal saveName As String, ByRef RecentList As System.Windows.Forms.AutoCompleteStringCollection) As Boolean
        RecentList.Clear()

        Dim strPath As String = Application.StartupPath & "\GRID\" & saveName & ".ini"
        If System.IO.File.Exists(strPath) = True Then
            Dim fs As New StreamReader(strPath, System.Text.Encoding.Default)
            Dim strLine As String = ""

            While fs.EndOfStream = False
                strLine = fs.ReadLine()
                If Not strLine Is Nothing AndAlso strLine.Length > 0 Then
                    RecentList.Add(strLine)
                End If
            End While

            fs.Close()
        End If

        Return True
    End Function

    Public Shared Function AddToRecentList(ByVal newItem As String, ByRef RecentList As System.Windows.Forms.AutoCompleteStringCollection) As Boolean
        If newItem Is Nothing OrElse newItem.Length = 0 Then Return False

        Dim upperItem As String = newItem.ToLower
        Dim isExist As Boolean = False
        For Each pItem As String In RecentList
            If upperItem = pItem.ToLower Then
                isExist = True
                Exit For
            End If
        Next

        If isExist = False Then
            RecentList.Insert(0, newItem)
            Return True
        End If

        Return False
    End Function


    Public Class SUB_LIST_INFO
        Public name As String = ""
        Public list As New System.Windows.Forms.AutoCompleteStringCollection
    End Class
    Public Shared Function LoadRecentSubList(ByVal parentName As String, ByRef subList As ArrayList) As Boolean
        Dim m_ParentList As New System.Windows.Forms.AutoCompleteStringCollection
        If LoadRecentList(parentName, m_ParentList) = True Then
            For Each pItem As String In m_ParentList
                Dim mSubListItem As New SUB_LIST_INFO
                mSubListItem.name = parentName & "_" & pItem
                LoadRecentList(mSubListItem.name, mSubListItem.list)
                subList.Add(mSubListItem)
            Next
        End If

        Return True
    End Function

    Public Shared Function SaveRecentSubList(ByRef subList As ArrayList) As Boolean
        For Each pItem As SUB_LIST_INFO In subList
            SaveRecentList(pItem.name, pItem.list)
        Next

        Return True
    End Function

    Public Shared Function AddToRecentSubList(ByVal parentName As String, ByVal subName As String, ByVal addItem As String, ByRef subList As ArrayList) As Boolean
        Dim addListName As String = parentName & "_" & subName
        For Each pItem As SUB_LIST_INFO In subList
            If pItem.name = addListName Then
                AddToRecentList(addItem, pItem.list)
            End If
        Next

        Return True
    End Function

    Public Shared Function GetRecentSubList(ByVal parentName As String, ByVal subName As String, ByRef subList As ArrayList, ByRef RecentList As System.Windows.Forms.AutoCompleteStringCollection) As Boolean
        Dim subItemName As String = parentName & "_" & subName
        RecentList = Nothing

        For Each pItem As SUB_LIST_INFO In subList
            If pItem.name = subItemName Then
                RecentList = pItem.list
            End If
        Next

        Return True
    End Function
End Class
