Module LoadRoutines
    Sub AddData()
        On Error Resume Next

        intFirstTab = 1
        intCurrentTab = 1

        If BuildTabs(intFirstTab) = ER_FATALERROR Then
            MsgBox(LE_FATALERROR, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        If ShowTabs(intFirstTab) = ER_FATALERROR Then
            MsgBox(LE_FATALERROR, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical + MsgBoxStyle.SystemModal, LS_MSGBOX_TITLE)
            End
        End If

        System.Windows.Forms.Application.DoEvents()

    End Sub
    Function BuildTabs(ByRef intStart As Short) As Short

        On Error Resume Next

        Dim intWidth As Short
        Dim i As Short
        Dim j As Short

        BuildTabs = ER_NOERROR

        ReDim intTabArray(intTotalTabs)

        For i = intStart To intTotalTabs
            For j = 0 To UBound(SEData) - 1
                If SEData(j).TypeID = i Then
                    intTabArray(i - 1) = SEData(j).PartID
                    Exit For
                End If
            Next

        Next

        If Err.Number Then
            BuildTabs = ER_FATALERROR
        End If

    End Function

    Function LoadClassList() As Short

        On Error Resume Next

        Dim i As Short

        frmSELibrary.cboClasses.Items.Clear()
        For i = LBound(SEData) To UBound(SEData) - 1

            If SEData(i).TypeID = intCurrentTab Then
                frmSELibrary.cboClasses.Items.Add(SEData(i).PartClass)
            End If

        Next
        System.Windows.Forms.Application.DoEvents()
        frmSELibrary.cboClasses.SelectedIndex = 0

    End Function
    Function LoadGrid(ByRef intPartID As Short) As Short

        On Error Resume Next

        Dim i, j, intVarCount, intSizeCount, nRow As Short
        Dim sinWidth As Single
        Dim tkey As String

        frmSELibrary.grdData.Rows.Clear()
        System.Windows.Forms.Application.DoEvents()

        frmSELibrary.grdData.Visible = True

        frmSELibrary.grdData.ColumnCount = SEData(intPartID).VariableCount
        frmSELibrary.grdData.RowCount = SEData(intPartID).SizeCount + 1

        frmSELibrary.grdData.ColumnHeadersVisible = True
        ' load the header
        For i = 0 To SEData(intPartID).VariableCount - 1
            frmSELibrary.grdData.Columns.Item(i).HeaderText = SEData(intPartID).Variables(i)
        Next

        intVarCount = SEData(intPartID).VariableCount - 1
        intSizeCount = SEData(intPartID).Sizes.Count
        ' load the data
        frmSELibrary.grdData.Rows.Add()

        j = -1
        nRow = 0
        i = 0
        For Each kvp As KeyValuePair(Of String, String) In SEData(intPartID).Sizes
            nRow = nRow + 1
            j = j + 1
            tkey = kvp.Key
            frmSELibrary.grdData.Rows.Item(i).Cells.Item(j).Value = kvp.Value
            If j = intVarCount Then
                j = -1
                i = i + 1
                If nRow < intSizeCount Then frmSELibrary.grdData.Rows.Add()
            End If
        Next

        frmSELibrary.grdData.AutoResizeColumns()
        frmSELibrary.grdData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        frmSELibrary.grdData.RowHeadersVisible = False
        frmSELibrary.grdData.ReadOnly = True
        frmSELibrary.grdData.Visible = True
        System.Windows.Forms.Application.DoEvents()

    End Function
End Module
