Imports System.Windows.Forms.DataVisualization.Charting
Imports System.IO
Public Class 物料採購統計表
    Dim 物料規格, 資料(100), 頁面 As String
    Dim 可繪圖 As Boolean
    Dim NA1, 關鍵字數量, 移動位置, 關鍵字位置(1000), 月數 As Integer
    Dim 群組碼(21) As String
    Private Sub Set_群組清單()
        Dim ds1 As New DataSet : 群組分類_dgv.DataSource = Nothing : ds1.Clear()
        群組分類_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        群組分類_dgv.ColumnHeadersHeight = 25 : 群組分類_dgv.AllowUserToAddRows = False : 群組分類_dgv.RowTemplate.Height = 45
        SQL_物料群組() : da.Fill(ds1) : 群組分類_dgv.DataSource = ds1.Tables(0) : conn.Close() : 次項變形()
    End Sub
    Private Sub Set_清單()
        Dim ds1 As New DataSet : 物料清單_dgv.DataSource = Nothing : ds1.Clear()
        物料清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        物料清單_dgv.ColumnHeadersHeight = 40 : 物料清單_dgv.AllowUserToAddRows = False : 物料清單_dgv.RowTemplate.Height = 45
        PA20 = 關鍵字查詢_tb.Text : Set_採購日清單()
        If 採購平均單價_ch.Checked = False And 採購數量_ch.Checked = True Then : SQL_採購數量清單()
        ElseIf 採購平均單價_ch.Checked = True And 採購數量_ch.Checked = False Then : SQL_採購平均單價清單()
        End If

        da.Fill(ds1) : 物料清單_dgv.DataSource = ds1.Tables(0) : conn.Close()
        物料清單_dgv.Columns(0).Visible = False : 物料清單_dgv.Columns(1).Width = 120 : 物料清單_dgv.Columns(2).Width = 170
        For I As Integer = 0 To 物料清單_dgv.Rows.Count - 1 : 物料清單_dgv.Rows(I).Cells(3).Value = "0"
            For II As Integer = 0 To 品名_dgv.Rows.Count - 1
                '-------------------------規格合併呈現----------------------------------------------------------------------------------------------------------------------------------------
                If 物料清單_dgv.Rows(I).Cells("料號").Value.ToString = 品名_dgv.Rows(II).Cells("料號").Value.ToString Then
                    PA3 = 品名_dgv.Rows(II).Cells("規格庫抬頭指定").Value : SQL_規格庫抬頭讀取()
                    If dr.Read() Then
                        AAA1 = dr("A1") : AAA2 = dr("A2") : AAA3 = dr("A3") : AAA4 = dr("A4") : AAA5 = dr("A5") : AAA6 = dr("A6") : AAA7 = dr("A7") : AAA8 = dr("A8")
                    End If : conn.Close()

                    Dim ZA1, ZA2, ZA3, ZA4, ZA5, ZA6, ZA7, ZA8 As String : 物料規格 = ""
                    If 詳細資料2_ch.Checked = True Then
                        If 品名_dgv.Rows(II).Cells("A1").Value.ToString <> "" Then
                            ZA1 = AAA1 & "=" & 品名_dgv.Rows(II).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                        If 品名_dgv.Rows(II).Cells("A2").Value.ToString <> "" Then : ZA2 = AAA2 & "=" & 品名_dgv.Rows(II).Cells("A2").Value
                            If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A3").Value.ToString <> "" Then : ZA3 = AAA3 & "=" & 品名_dgv.Rows(II).Cells("A3").Value
                            If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A4").Value.ToString <> "" Then : ZA4 = AAA4 & "=" & 品名_dgv.Rows(II).Cells("A4").Value
                            If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A5").Value.ToString <> "" Then : ZA5 = AAA5 & "=" & 品名_dgv.Rows(II).Cells("A5").Value
                            If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A6").Value.ToString <> "" Then : ZA6 = AAA6 & "=" & 品名_dgv.Rows(II).Cells("A6").Value
                            If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A7").Value.ToString <> "" Then : ZA7 = AAA7 & "=" & 品名_dgv.Rows(II).Cells("A7").Value
                            If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A8").Value.ToString <> "" Then : ZA8 = AAA8 & "=" & 品名_dgv.Rows(II).Cells("A8").Value
                            If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                    Else
                        If 品名_dgv.Rows(II).Cells("A1").Value.ToString <> "" Then : ZA1 = 品名_dgv.Rows(II).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                        If 品名_dgv.Rows(II).Cells("A2").Value.ToString <> "" Then
                            ZA2 = 品名_dgv.Rows(II).Cells("A2").Value : If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A3").Value.ToString <> "" Then
                            ZA3 = 品名_dgv.Rows(II).Cells("A3").Value : If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A4").Value.ToString <> "" Then
                            ZA4 = 品名_dgv.Rows(II).Cells("A4").Value : If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A5").Value.ToString <> "" Then
                            ZA5 = 品名_dgv.Rows(II).Cells("A5").Value : If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A6").Value.ToString <> "" Then
                            ZA6 = 品名_dgv.Rows(II).Cells("A6").Value : If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A7").Value.ToString <> "" Then
                            ZA7 = 品名_dgv.Rows(II).Cells("A7").Value : If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                        If 品名_dgv.Rows(II).Cells("A8").Value.ToString <> "" Then
                            ZA8 = 品名_dgv.Rows(II).Cells("A8").Value : If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                    End If
                    物料清單_dgv.Rows(I).Cells(2).Value = 物料規格 : Exit For
                End If
            Next
            If 月數 > 0 Then
                For II As Integer = 4 To 3 + 月數
                    If 物料清單_dgv.Rows(I).Cells(II).Value.ToString = "" Then : 物料清單_dgv.Rows(I).Cells(II).Value = "0" : End If
                    物料清單_dgv.Rows(I).Cells(3).Value = CInt(物料清單_dgv.Rows(I).Cells(3).Value.ToString) + CInt(物料清單_dgv.Rows(I).Cells(II).Value.ToString)
                Next
            End If
        Next
        If 採購平均單價_ch.Checked = False And 採購數量_ch.Checked = True Then
            物料清單_dgv.Columns(3).FillWeight = 50 : 物料清單_dgv.Columns(3).Visible = True
        ElseIf 採購平均單價_ch.Checked = True And 採購數量_ch.Checked = False Then
            物料清單_dgv.Columns(3).FillWeight = 50 : 物料清單_dgv.Columns(3).Visible = False
        End If
        For II As Integer = 4 To NN1 + 4
            物料清單_dgv.Columns(II).FillWeight = 50 : 物料清單_dgv.Columns(II).DefaultCellStyle.Format = "#,##0"
            物料清單_dgv.Columns(II).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Next
        物料清單_dgv.Columns("總採購量").DefaultCellStyle.Format = "#,##0"
        物料清單_dgv.Columns("總採購量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        物料清單_dgv.Sort(物料清單_dgv.Columns(0), System.ComponentModel.ListSortDirection.Descending)
    End Sub
    Private Sub Set_採購日清單()
        Dim ds1 As New DataSet : 採購日_dgv.DataSource = Nothing : ds1.Clear()
        採購日_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        採購日_dgv.ColumnHeadersHeight = 25 : 採購日_dgv.AllowUserToAddRows = False
        SQL_採購日清單()
        da.Fill(ds1) : 採購日_dgv.DataSource = ds1.Tables(0) : conn.Close()
        採購日_dgv.Sort(採購日_dgv.Columns("採購日"), System.ComponentModel.ListSortDirection.Ascending)
        PA10 = 採購日_dgv.Rows(0).Cells("採購日").Value.ToString() : 月數 = 0
        For I As Integer = 0 To 採購日_dgv.Rows.Count - 1
            If I = 0 Then : 清單列表 = "[" & 採購日_dgv.Rows(I).Cells(0).Value.ToString()
            Else : 清單列表 = 清單列表 & "], [" & 採購日_dgv.Rows(I).Cells(0).Value.ToString() : End If
            If I = 採購日_dgv.Rows.Count - 1 Then : 清單列表 &= "]" : End If : NN1 = I
            PA11 = 採購日_dgv.Rows(I).Cells("採購日").Value.ToString() : 月數 += 1
        Next
    End Sub
    Private Sub Set_品名清單()
        Dim ds1 As New DataSet : 品名_dgv.DataSource = Nothing : ds1.Clear()
        品名_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        品名_dgv.ColumnHeadersHeight = 25 : 品名_dgv.AllowUserToAddRows = False
        SQL_品名清單()
        da.Fill(ds1) : 品名_dgv.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_繪圖清單()
        Dim ds1 As New DataSet : 繪圖1_dgv.DataSource = Nothing : ds1.Clear()
        繪圖1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        繪圖1_dgv.ColumnHeadersHeight = 25 : 繪圖1_dgv.AllowUserToAddRows = False
        SQL_繪圖清單1()
        da.Fill(ds1) : 繪圖1_dgv.DataSource = ds1.Tables(0) : conn.Close()
        For i As Integer = 0 To 繪圖1_dgv.Rows.Count - 1
            If IsDBNull(繪圖1_dgv.Rows(i).Cells("採購數量").Value) = True Then
                繪圖1_dgv.Rows(i).Cells("採購數量").Value = "0" : 繪圖1_dgv.Rows(i).Cells("料號").Value = PA1 : 繪圖1_dgv.Rows(i).Cells("平均單價").Value = "0"
            End If
        Next
    End Sub
    Private Sub 關鍵字查詢_bt_MouseEnter(sender As Object, e As EventArgs) Handles 關鍵字查詢_bt.MouseEnter
        ToolTip1.SetToolTip(Me.關鍵字查詢_bt, "關鍵字查詢")
    End Sub
    Private Sub 上一筆_bt_MouseEnter(sender As Object, e As EventArgs) Handles 上一筆_bt.MouseEnter
        ToolTip1.SetToolTip(Me.上一筆_bt, "上一筆")
    End Sub
    Private Sub 下一筆_bt_MouseEnter(sender As Object, e As EventArgs) Handles 下一筆_bt.MouseEnter
        ToolTip1.SetToolTip(Me.下一筆_bt, "下一筆")
    End Sub
    Private Sub 查詢入圖_bt_MouseEnter(sender As Object, e As EventArgs) Handles 查詢入圖_bt.MouseEnter
        ToolTip1.SetToolTip(Me.查詢入圖_bt, "查詢資料生成圖表")
    End Sub
    Private Sub 清除查詢_bt_MouseEnter(sender As Object, e As EventArgs) Handles 清除查詢_bt.MouseEnter
        ToolTip1.SetToolTip(Me.清除查詢_bt, "清除查詢")
    End Sub
    Private Sub 清除圖表_bt_MouseEnter(sender As Object, e As EventArgs) Handles 清除圖表_bt.MouseEnter
        ToolTip1.SetToolTip(Me.清除圖表_bt, "清除圖表")
    End Sub
    Private Sub 回首頁_bt_MouseEnter(sender As Object, e As EventArgs) Handles 回首頁_bt.MouseEnter
        ToolTip1.SetToolTip(Me.回首頁_bt, "開啟物料資料庫查詢")
    End Sub
    Private Sub 物料採購統計表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = FH_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Panel1.SendToBack() : Chart1.Visible = False : ToolTip1.ToolTipTitle = "系統說明"
        物料採購統計表_Siz()
    End Sub
    Private Sub 物料採購統計表_Siz()
        物料清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 繪圖()
        If 折線圖_ch.Checked = True Then
            Chart1.Series.Add(資料(NA1) & "_1") : Chart1.Series(資料(NA1) & "_1").ChartType = SeriesChartType.Line
            Chart1.Series(資料(NA1) & "_1").IsVisibleInLegend = False
        End If
        If 長條圖_ch.Checked = True Then
            Chart1.Series.Add(資料(NA1)) : Chart1.Series(資料(NA1)).ChartType = SeriesChartType.Column
        End If
        Chart1.ChartAreas(0).BackColor = Color.Transparent : Chart1.Legends(0).BackColor = Color.Transparent
        Chart1.ChartAreas(0).AxisY.LabelStyle.Font = New Font("Arial", 8) : Chart1.ChartAreas(0).AxisY.LabelStyle.Format = "#,##0"
        Dim NUDAA As Integer
        If 數列_NUD.Value >= 繪圖1_dgv.Rows.Count - 1 Or 數列_NUD.Value = 0 Then : NUDAA = 繪圖1_dgv.Rows.Count - 1
        ElseIf 數列_NUD.Value < 繪圖1_dgv.Rows.Count - 1 Then : NUDAA = 數列_NUD.Value : End If
        For II As Integer = 0 To NUDAA
            If 採購平均單價_ch.Checked = False And 採購數量_ch.Checked = True Then
                If 長條圖_ch.Checked = True Then
                    Chart1.Series(資料(NA1)).Points.AddXY(繪圖1_dgv.Rows(NUDAA - II).Cells("採購日").Value.ToString, 繪圖1_dgv.Rows(NUDAA - II).Cells("採購數量").Value.ToString)
                End If
                If 折線圖_ch.Checked = True Then
                    Chart1.Series(資料(NA1) & "_1").Points.AddXY(繪圖1_dgv.Rows(NUDAA - II).Cells("採購日").Value.ToString, 繪圖1_dgv.Rows(NUDAA - II).Cells("採購數量").Value.ToString)
                End If
            ElseIf 採購平均單價_ch.Checked = True And 採購數量_ch.Checked = False Then
                If 長條圖_ch.Checked = True Then
                    Chart1.Series(資料(NA1)).Points.AddXY(繪圖1_dgv.Rows(NUDAA - II).Cells("採購日").Value.ToString, 繪圖1_dgv.Rows(NUDAA - II).Cells("平均單價").Value.ToString)
                End If
                If 折線圖_ch.Checked = True Then
                    Chart1.Series(資料(NA1) & "_1").Points.AddXY(繪圖1_dgv.Rows(NUDAA - II).Cells("採購日").Value.ToString, 繪圖1_dgv.Rows(NUDAA - II).Cells("平均單價").Value.ToString)
                End If
            End If
        Next : NA1 = NA1 + 1
        Chart1.Legends(0).ForeColor = Color.Black
        Chart1.ChartAreas(0).AxisX.MajorGrid.LineColor = Color.Black : Chart1.ChartAreas(0).AxisY.MajorGrid.LineColor = Color.Black
        Chart1.ChartAreas(0).AxisX.LineColor = Color.Black : Chart1.ChartAreas(0).AxisY.LineColor = Color.Black
        Chart1.ChartAreas(0).AxisX.LabelStyle.ForeColor = Color.Black : Chart1.ChartAreas(0).AxisY.LabelStyle.ForeColor = Color.Black
    End Sub
    Private Sub 物料清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 物料清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            PA1 = 物料清單_dgv.Rows(e.RowIndex).Cells("料號").Value.ToString : Set_繪圖清單()
            If 繪圖1_dgv.Rows.Count > 0 Then
                For i As Integer = 0 To NA1
                    可繪圖 = True
                    If 資料(i) = 物料清單_dgv.Rows(e.RowIndex).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(e.RowIndex).Cells("規格").Value.ToString Then
                        可繪圖 = False : Exit For
                    End If
                Next
                If 可繪圖 = True Then
                    Try
                        資料(NA1) = 物料清單_dgv.Rows(e.RowIndex).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(e.RowIndex).Cells("規格").Value.ToString : 繪圖()
                    Catch ex As Exception : MsgBox(ex.Message) : End Try
                End If
            End If
        End If
    End Sub
    Private Sub 查詢入圖_bt_Click(sender As Object, e As EventArgs) Handles 查詢入圖_bt.Click
        Chart1.Series.Clear()
        For I As Integer = 0 To 關鍵字數量
            If 關鍵字位置(I) = 1 Then
                PA1 = 物料清單_dgv.Rows(I).Cells("料號").Value.ToString : Set_繪圖清單()
                If 繪圖1_dgv.Rows.Count > 0 Then
                    For II As Integer = 0 To NA1
                        可繪圖 = True
                        If 資料(II) = 物料清單_dgv.Rows(I).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(I).Cells("規格").Value.ToString Then
                            可繪圖 = False : Exit For
                        End If
                    Next
                    If 可繪圖 = True Then
                        Try
                            資料(NA1) = 物料清單_dgv.Rows(I).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(I).Cells("規格").Value.ToString : 繪圖()
                        Catch ex As Exception : MsgBox(ex.Message) : End Try
                    End If
                End If
            End If
        Next
    End Sub
    Private Sub 詳細資料2_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 詳細資料2_ch.Click
        If 詳細資料2_ch.Checked = True Then : 詳細資料2_ch.Checked = True : Else : 詳細資料2_ch.Checked = False : End If : Set_清單()
    End Sub
    Private Sub 採購平均單價_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 採購平均單價_ch.Click
        採購平均單價_ch.Checked = True : 採購數量_ch.Checked = False : 清除圖表_bt.PerformClick() : Set_清單()
    End Sub
    Private Sub 採購數量_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 採購數量_ch.Click
        採購平均單價_ch.Checked = False : 採購數量_ch.Checked = True : 清除圖表_bt.PerformClick() : Set_清單()
    End Sub
    Private Sub 關鍵字查詢_bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 關鍵字查詢_bt.Click
        If 關鍵字查詢_tb.Text = "" Then : MsgBox("關鍵字不可空白!!") : Else : PA2 = "%"
            Set_清單() : 關鍵字數量 = 0 : For I As Integer = 0 To 1000 : 關鍵字位置(I) = 0 : Next
            Dim Str1, Str2 As String
            For I As Integer = 0 To 物料清單_dgv.Rows.Count - 1
                物料清單_dgv.Rows(I).DefaultCellStyle.BackColor = Color.White
                Str1 = 物料清單_dgv.Rows(I).Cells("品名").Value.ToString() : Str2 = 關鍵字查詢_tb.Text
                Dim Stu As Boolean = Str1.Contains(Str2)
                If Stu = True Then
                    物料清單_dgv.Rows(I).DefaultCellStyle.BackColor = Color.LightPink : 關鍵字位置(I) = 1 : 關鍵字數量 = I : 移動位置 = 0
                End If
            Next : 下一筆_bt.PerformClick()
        End If
    End Sub
    Private Sub 長條圖_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 長條圖_ch.Click
        If 長條圖_ch.Checked = False And 折線圖_ch.Checked = False Then : 長條圖_ch.Checked = True : End If
    End Sub
    Private Sub 折線圖_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 折線圖_ch.Click
        If 長條圖_ch.Checked = False And 折線圖_ch.Checked = False Then : 折線圖_ch.Checked = True : End If
    End Sub
    Private Sub 清除查詢_bt_Click(sender As Object, e As EventArgs) Handles 清除查詢_bt.Click
        關鍵字數量 = 0 : For I As Integer = 0 To 1000 : 關鍵字位置(I) = 0 : Next : 清除圖表_bt.PerformClick()
        關鍵字查詢_tb.Text = "%%" : 關鍵字查詢_bt.PerformClick() : 關鍵字查詢_tb.Text = ""
        For I As Integer = 0 To 物料清單_dgv.Rows.Count - 1
            物料清單_dgv.Rows(I).DefaultCellStyle.BackColor = Color.White
        Next
    End Sub
    Private Sub 上一筆_bt_Click(sender As Object, e As EventArgs) Handles 上一筆_bt.Click
        If 移動位置 = 0 Then : MsgBox("已經是最前面!!") : Else
            Dim 減項 As Integer = 移動位置
            For I As Integer = 0 To 移動位置
                減項 -= 1
                If 關鍵字位置(減項) = 1 Then : 物料清單_dgv.CurrentCell = 物料清單_dgv.Rows(減項).Cells(1) : 移動位置 = 減項 : Exit For : End If
                If 減項 = 0 Then : 移動位置 = 0 : Exit Sub : End If
            Next
        End If
    End Sub
    Private Sub 下一筆_bt_Click(sender As Object, e As EventArgs) Handles 下一筆_bt.Click
        If 移動位置 >= 關鍵字數量 Then : MsgBox("已經是最後面!!") : Else
            For I As Integer = 移動位置 To 關鍵字數量
                If 關鍵字位置(I) = 1 Then : 物料清單_dgv.CurrentCell = 物料清單_dgv.Rows(I).Cells(1) : 移動位置 = I + 1 : Exit For : End If
                If 移動位置 = 關鍵字數量 Then : 移動位置 = 關鍵字數量 : End If
            Next
        End If
    End Sub
    Private Sub 清除圖表_bt_Click(sender As Object, e As EventArgs) Handles 清除圖表_bt.Click
        Chart1.Series.Clear() : For i As Integer = 0 To NA1 : 資料(i) = "" : Next : NA1 = 0
    End Sub
    Private Sub 回首頁_bt_Click(sender As Object, e As EventArgs) Handles 回首頁_bt.Click
        關鍵字查詢_tb.Text = "" : 市構件分類.ShowDialog()
    End Sub
    Private Sub 讀取_bt_Click(sender As Object, e As EventArgs) Handles 讀取_bt.Click
        PA2 = PA1 : Set_品名清單() : Chart1.Visible = True : Set_清單() : NA1 = 0 : 載入時繪圖()
    End Sub
    Private Sub 次項變形()
        For i As Integer = 1 To 21 : Dim 選擇 As Panel = CType(Me.Controls.Find("AP" & i, True)(0), Panel) : 選擇.Visible = False : Next
        Dim SS As Integer
        If 群組分類_dgv.Rows.Count > 21 Then : SS = 21 : Else : SS = 群組分類_dgv.Rows.Count : End If
        For i As Integer = 1 To SS : Dim 選擇 As Panel = CType(Me.Controls.Find("AP" & i, True)(0), Panel) : 選擇.Visible = True : Next
        Dim 轉檔 As Boolean
        For i As Integer = 0 To 群組分類_dgv.Rows.Count - 1
            Dim 選擇 As PictureBox = CType(Me.Controls.Find("PB_" & i + 1, True)(0), PictureBox)
            If IsDBNull(群組分類_dgv.Rows(i).Cells(2).Value) = True Then : 轉檔 = False : Else : 轉檔 = True : End If
            While 轉檔 = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(群組分類_dgv.Rows(i).Cells(2).Value, Byte())
                Dim oStream As New MemoryStream(bytes)
                選擇.Image = Bitmap.FromStream(oStream) : 轉檔 = False
            End While : conn.Close() : 選擇.SizeMode = 4
            Dim 選擇1 As TextBox = CType(Me.Controls.Find("TB_" & i + 1, True)(0), TextBox) : 選擇1.Text = 群組分類_dgv.Rows(i).Cells(1).Value.ToString
            '---------未做換頁計算---------------------------------------------
            群組碼(i + 1) = 群組分類_dgv.Rows(i).Cells(0).Value.ToString
        Next
    End Sub
    Private Sub 載入時繪圖()
        Chart1.Series.Clear()
        If 物料清單_dgv.Rows.Count > 0 Then : PA1 = 物料清單_dgv.Rows(0).Cells("料號").Value.ToString : Set_繪圖清單()
            If 繪圖1_dgv.Rows.Count > 0 Then
                For i As Integer = 0 To NA1
                    可繪圖 = True
                    If 資料(i) = 物料清單_dgv.Rows(0).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(0).Cells("規格").Value.ToString Then : 可繪圖 = False : Exit For : End If
                Next
                If 可繪圖 = True Then
                    Try
                        資料(NA1) = 物料清單_dgv.Rows(0).Cells("品名").Value.ToString & " " & 物料清單_dgv.Rows(0).Cells("規格").Value.ToString : 繪圖()
                    Catch ex As Exception : MsgBox(ex.Message) : End Try
                End If
            End If
        End If
    End Sub
End Class