Imports System.IO
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Public Class 專案項目BOM表
    Dim ds4 As New DataSet
    Dim KKK2, 物料規格, ESTR, 目前選擇, 條件選擇, 對話框(20) As String
    Dim 啟動運算, 已超出, 放大, 不再動作1, 不再動作2, 查詢 As Boolean
    Dim EDR, 頁數, DGVX, NUM1 As Integer
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim file_name, 文件判斷 As String
    Dim Str As String = Application.StartupPath
    Private Sub Set_BOM表控制1()
        Dim ds6 As New DataSet
        BOM表控制1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        BOM表控制1_dgv.ColumnHeadersHeight = 25 : BOM表控制1_dgv.AllowUserToAddRows = False : BOM表控制1_dgv.RowTemplate.Height = 25
        Dim 群組碼 As String
        If 群組碼1_cb.Text = "%%" Then : 群組碼 = " WHERE (群組碼 IS NULL) OR (群組碼 LIKE N'%%')  " : Else : 群組碼 = " WHERE (群組碼 LIKE N'" & 群組碼1_cb.Text & "')  " : End If
        SQL_BOM表控制(群組碼)
        da.Fill(ds6) : BOM表控制1_dgv.DataSource = ds6.Tables(0) : conn.Close()
        BOM表控制1_dgv.Columns(0).Visible = False : BOM表控制1_dgv.Columns(1).FillWeight = 100
        BOM表控制1_dgv.Columns(0).ReadOnly = True : BOM表控制1_dgv.Columns(1).ReadOnly = True
    End Sub
    Private Sub Set_合約報價明細表()
        If BOM表明細1_dgv.Columns.Count.ToString > 41 Then : BOM表明細1_dgv.Columns.Remove("選擇") : End If
        Dim ds6 As New DataSet
        BOM表明細1_dgv.DataSource = Nothing : ds6.Clear()
        BOM表明細1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        BOM表明細1_dgv.ColumnHeadersHeight = 25 : BOM表明細1_dgv.AllowUserToAddRows = False : BOM表明細1_dgv.RowTemplate.Height = 25
        SQL_BOM表明細()
        da.Fill(ds6) : BOM表明細1_dgv.DataSource = ds6.Tables(0) : conn.Close() : Dim SD As Integer = 10
        BOM表明細1_dgv.Columns("排序").FillWeight = 30 : BOM表明細1_dgv.Columns("排序").Visible = False : BOM表明細1_dgv.Columns("項1").FillWeight = SD
        BOM表明細1_dgv.Columns("項2").FillWeight = SD : BOM表明細1_dgv.Columns("項3").FillWeight = SD : BOM表明細1_dgv.Columns("項4").FillWeight = SD
        BOM表明細1_dgv.Columns("項5").FillWeight = SD : BOM表明細1_dgv.Columns("項6").FillWeight = SD
        BOM表明細1_dgv.Columns("項目").FillWeight = 150 : BOM表明細1_dgv.Columns("單位").FillWeight = 30 : BOM表明細1_dgv.Columns("數量").FillWeight = 30
        BOM表明細1_dgv.Columns("料號").FillWeight = 75 : BOM表明細1_dgv.Columns(6).Visible = False
        For i As Integer = 11 To 40 : BOM表明細1_dgv.Columns(i).Visible = False : Next
        For i As Integer = 0 To 40 : BOM表明細1_dgv.Columns(i).ReadOnly = True : Next
        BOM表明細1_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0"
        BOM表明細1_dgv.Columns("項1").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細1_dgv.Columns("項2").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細1_dgv.Columns("項3").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細1_dgv.Columns("項4").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細1_dgv.Columns("項5").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細1_dgv.Columns("項6").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        For i As Integer = 9 To 10 : BOM表明細1_dgv.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : Next

        For i As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1
            If BOM表明細1_dgv.Rows(i).Cells("項1").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項1").Value = 1 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項2").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項2").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項3").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項4").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項5").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項6").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 : End If

            If BOM表明細1_dgv.Rows(i).Cells("項2").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And
                BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Red
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And
                BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Peru
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Green
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Blue
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black
            Else : BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black : End If

            If BOM表明細1_dgv.Rows(i).Cells("物料單位").Value.ToString <> "" Then : BOM表明細1_dgv.Rows(i).Cells("單位").Value = BOM表明細1_dgv.Rows(i).Cells("物料單位").Value.ToString : End If
            If BOM表明細1_dgv.Rows(i).Cells("料號").Value.ToString <> "" Then
                Dim ZA1, ZA2, ZA3, ZA4, ZA5, ZA6, ZA7, ZA8 As String : 物料規格 = ""
                If 詳細資料2_ch.Checked = True Then
                    If BOM表明細1_dgv.Rows(i).Cells("A1").Value.ToString <> "" Then
                        ZA1 = BOM表明細1_dgv.Rows(i).Cells("AA1").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A2").Value.ToString <> "" Then
                        ZA2 = BOM表明細1_dgv.Rows(i).Cells("AA2").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A2").Value
                        If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A3").Value.ToString <> "" Then
                        ZA3 = BOM表明細1_dgv.Rows(i).Cells("AA3").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A3").Value
                        If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A4").Value.ToString <> "" Then
                        ZA4 = BOM表明細1_dgv.Rows(i).Cells("AA4").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A4").Value
                        If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A5").Value.ToString <> "" Then
                        ZA5 = BOM表明細1_dgv.Rows(i).Cells("AA5").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A5").Value
                        If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A6").Value.ToString <> "" Then
                        ZA6 = BOM表明細1_dgv.Rows(i).Cells("AA6").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A6").Value
                        If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A7").Value.ToString <> "" Then
                        ZA7 = BOM表明細1_dgv.Rows(i).Cells("AA7").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A7").Value
                        If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A8").Value.ToString <> "" Then
                        ZA8 = BOM表明細1_dgv.Rows(i).Cells("AA8").Value.ToString & "=" & BOM表明細1_dgv.Rows(i).Cells("A8").Value
                        If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                Else
                    If BOM表明細1_dgv.Rows(i).Cells("A1").Value.ToString <> "" Then : ZA1 = BOM表明細1_dgv.Rows(i).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A2").Value.ToString <> "" Then
                        ZA2 = BOM表明細1_dgv.Rows(i).Cells("A2").Value : If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A3").Value.ToString <> "" Then
                        ZA3 = BOM表明細1_dgv.Rows(i).Cells("A3").Value : If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A4").Value.ToString <> "" Then
                        ZA4 = BOM表明細1_dgv.Rows(i).Cells("A4").Value : If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A5").Value.ToString <> "" Then
                        ZA5 = BOM表明細1_dgv.Rows(i).Cells("A5").Value : If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A6").Value.ToString <> "" Then
                        ZA6 = BOM表明細1_dgv.Rows(i).Cells("A6").Value : If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A7").Value.ToString <> "" Then
                        ZA7 = BOM表明細1_dgv.Rows(i).Cells("A7").Value : If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                    If BOM表明細1_dgv.Rows(i).Cells("A8").Value.ToString <> "" Then
                        ZA8 = BOM表明細1_dgv.Rows(i).Cells("A8").Value : If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                End If
                If BOM表明細1_dgv.Rows(i).Cells("料號").Value.ToString <> "" Then
                    BOM表明細1_dgv.Rows(i).Cells("項目").Value = BOM表明細1_dgv.Rows(i).Cells("品名").Value.ToString & " " & 物料規格
                End If
            End If
        Next

        Set_控制表_dgv_插入勾選控件清單() : BOM表明細1_dgv.Columns("選擇").FillWeight = 20
        For I As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1 : BOM表明細1_dgv.Rows(I).Cells("選擇").Value = False : Next
    End Sub
    Private Sub Set_控制表_dgv_插入勾選控件清單()
        Dim Col As New DataGridViewCheckBoxColumn With {
            .FillWeight = 20,
            .DataPropertyName = "選擇"
        }
        Col.HeaderText = "選擇" : Col.Name = "選擇"
        BOM表明細1_dgv.Columns.Insert(0, Col)
    End Sub
    Private Sub Set_BOM表控制()
        Dim ds6 As New DataSet
        BOM表控制_dgv.DataSource = Nothing : ds6.Clear()
        BOM表控制_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        BOM表控制_dgv.ColumnHeadersHeight = 25 : BOM表控制_dgv.AllowUserToAddRows = False : BOM表控制_dgv.RowTemplate.Height = 45
        Dim 群組碼 As String
        If 群組碼_cb.Text = "%%" Then : 群組碼 = " WHERE (群組碼 IS NULL) OR (群組碼 LIKE N'%%')  " : Else : 群組碼 = " WHERE (群組碼 LIKE N'" & 群組碼_cb.Text & "')  " : End If
        SQL_BOM表控制(群組碼)
        da.Fill(ds6) : BOM表控制_dgv.DataSource = ds6.Tables(0) : conn.Close()
        BOM表控制_dgv.Columns(0).Visible = False : BOM表控制_dgv.Columns(1).FillWeight = 100
        BOM表控制_dgv.Columns(0).ReadOnly = True : BOM表控制_dgv.Columns(1).ReadOnly = True
    End Sub
    Private Sub Set_BOM表明細()
        SQL_物料資料庫_自動修正單位()
        Dim ds6 As New DataSet
        BOM表明細_dgv.DataSource = Nothing : ds6.Clear()
        BOM表明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        BOM表明細_dgv.ColumnHeadersHeight = 25 : BOM表明細_dgv.AllowUserToAddRows = False : BOM表明細_dgv.RowTemplate.Height = 25
        SQL_BOM表明細()
        da.Fill(ds6) : BOM表明細_dgv.DataSource = ds6.Tables(0) : conn.Close() : Dim SD As Integer = 10
        BOM表明細_dgv.Columns("排序").FillWeight = 30 : BOM表明細_dgv.Columns("排序").Visible = False : BOM表明細_dgv.Columns("項1").FillWeight = SD
        BOM表明細_dgv.Columns("項2").FillWeight = SD : BOM表明細_dgv.Columns("項3").FillWeight = SD : BOM表明細_dgv.Columns("項4").FillWeight = SD
        BOM表明細_dgv.Columns("項5").FillWeight = SD : BOM表明細_dgv.Columns("項6").FillWeight = SD
        BOM表明細_dgv.Columns("項目").FillWeight = 200 : BOM表明細_dgv.Columns("單位").FillWeight = 30 : BOM表明細_dgv.Columns("數量").FillWeight = 30
        BOM表明細_dgv.Columns("料號").FillWeight = 50 : BOM表明細_dgv.Columns(6).Visible = False
        For i As Integer = 11 To 40 : BOM表明細_dgv.Columns(i).Visible = False : Next
        BOM表明細_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0"
        BOM表明細_dgv.Columns("項1").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.Columns("項2").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.Columns("項3").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.Columns("項4").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.Columns("項5").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.Columns("項6").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        BOM表明細_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        BOM表明細_dgv.Columns("料號").ReadOnly = True
        For i As Integer = 9 To 10 : BOM表明細_dgv.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : Next

        For i As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
            If BOM表明細_dgv.Rows(i).Cells("項1").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項1").Value = 1 : End If
            If BOM表明細_dgv.Rows(i).Cells("項2").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項2").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項3").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項4").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項5").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項6").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 : End If

            If BOM表明細_dgv.Rows(i).Cells("項2").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And
                BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Red
            ElseIf BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And
                BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Peru
            ElseIf BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Green
            ElseIf BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Blue
            ElseIf BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black
            Else : BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black : End If

            If BOM表明細_dgv.Rows(i).Cells("物料單位").Value.ToString <> "" Then : BOM表明細_dgv.Rows(i).Cells("單位").Value = BOM表明細_dgv.Rows(i).Cells("物料單位").Value.ToString : End If
            If BOM表明細_dgv.Rows(i).Cells("料號").Value.ToString <> "" Then
                Dim ZA1, ZA2, ZA3, ZA4, ZA5, ZA6, ZA7, ZA8 As String : 物料規格 = ""
                If 詳細資料2_ch.Checked = True Then
                    If BOM表明細_dgv.Rows(i).Cells("A1").Value.ToString <> "" Then
                        ZA1 = BOM表明細_dgv.Rows(i).Cells("AA1").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                    If BOM表明細_dgv.Rows(i).Cells("A2").Value.ToString <> "" Then
                        ZA2 = BOM表明細_dgv.Rows(i).Cells("AA2").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A2").Value
                        If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A3").Value.ToString <> "" Then
                        ZA3 = BOM表明細_dgv.Rows(i).Cells("AA3").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A3").Value
                        If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A4").Value.ToString <> "" Then
                        ZA4 = BOM表明細_dgv.Rows(i).Cells("AA4").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A4").Value
                        If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A5").Value.ToString <> "" Then
                        ZA5 = BOM表明細_dgv.Rows(i).Cells("AA5").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A5").Value
                        If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A6").Value.ToString <> "" Then
                        ZA6 = BOM表明細_dgv.Rows(i).Cells("AA6").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A6").Value
                        If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A7").Value.ToString <> "" Then
                        ZA7 = BOM表明細_dgv.Rows(i).Cells("AA7").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A7").Value
                        If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A8").Value.ToString <> "" Then
                        ZA8 = BOM表明細_dgv.Rows(i).Cells("AA8").Value.ToString & "=" & BOM表明細_dgv.Rows(i).Cells("A8").Value
                        If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                Else
                    If BOM表明細_dgv.Rows(i).Cells("A1").Value.ToString <> "" Then : ZA1 = BOM表明細_dgv.Rows(i).Cells("A1").Value.ToString : 物料規格 = ZA1 : End If
                    If BOM表明細_dgv.Rows(i).Cells("A2").Value.ToString <> "" Then
                        ZA2 = BOM表明細_dgv.Rows(i).Cells("A2").Value : If 物料規格 = "" Then : 物料規格 = ZA2 : Else : 物料規格 = 物料規格 + "、" + ZA2 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A3").Value.ToString <> "" Then
                        ZA3 = BOM表明細_dgv.Rows(i).Cells("A3").Value : If 物料規格 = "" Then : 物料規格 = ZA3 : Else : 物料規格 = 物料規格 + "、" + ZA3 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A4").Value.ToString <> "" Then
                        ZA4 = BOM表明細_dgv.Rows(i).Cells("A4").Value : If 物料規格 = "" Then : 物料規格 = ZA4 : Else : 物料規格 = 物料規格 + "、" + ZA4 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A5").Value.ToString <> "" Then
                        ZA5 = BOM表明細_dgv.Rows(i).Cells("A5").Value : If 物料規格 = "" Then : 物料規格 = ZA5 : Else : 物料規格 = 物料規格 + "、" + ZA5 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A6").Value.ToString <> "" Then
                        ZA6 = BOM表明細_dgv.Rows(i).Cells("A6").Value : If 物料規格 = "" Then : 物料規格 = ZA6 : Else : 物料規格 = 物料規格 + "、" + ZA6 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A7").Value.ToString <> "" Then
                        ZA7 = BOM表明細_dgv.Rows(i).Cells("A7").Value : If 物料規格 = "" Then : 物料規格 = ZA7 : Else : 物料規格 = 物料規格 + "、" + ZA7 : End If : End If
                    If BOM表明細_dgv.Rows(i).Cells("A8").Value.ToString <> "" Then
                        ZA8 = BOM表明細_dgv.Rows(i).Cells("A8").Value : If 物料規格 = "" Then : 物料規格 = ZA8 : Else : 物料規格 = 物料規格 + "、" + ZA8 : End If : End If
                End If
                If BOM表明細_dgv.Rows(i).Cells("料號").Value.ToString <> "" Then
                    BOM表明細_dgv.Rows(i).Cells("項目").Value = BOM表明細_dgv.Rows(i).Cells("品名").Value.ToString & " " & 物料規格
                End If
            End If
        Next
    End Sub
    Private Sub 載入群組碼()
        SQL_工程項目BOM_群組碼()
        群組碼_cb.Items.Clear() : 群組碼中_cb.Items.Clear() : 群組碼印_cb.Items.Clear()
        群組碼1_cb.Items.Clear() : 群組碼中1_cb.Items.Clear() : 群組碼印1_cb.Items.Clear()
        群組碼_cb.Items.Add("%%") : 群組碼中_cb.Items.Add("未分配群組") : 群組碼印_cb.Items.Add("Grup Belum Ditugaskan")
        群組碼1_cb.Items.Add("%%") : 群組碼中1_cb.Items.Add("未分配群組") : 群組碼印1_cb.Items.Add("Grup Belum Ditugaskan")
        While (dr.Read())
            群組碼_cb.Items.Add(dr("群組碼")) : 群組碼中_cb.Items.Add(dr("中文名稱")) : 群組碼印_cb.Items.Add(dr("印尼文名稱"))
            群組碼1_cb.Items.Add(dr("群組碼")) : 群組碼中1_cb.Items.Add(dr("中文名稱")) : 群組碼印1_cb.Items.Add(dr("印尼文名稱"))
        End While
    End Sub
    Private Sub Set_DGV1載入前設定()
        物料清單_dgv.DataSource = Nothing : ds4.Clear()
        物料清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        物料清單_dgv.ColumnHeadersHeight = 25 : 物料清單_dgv.AllowUserToAddRows = False : 物料清單_dgv.RowTemplate.Height = 45
        ConnOpen()
    End Sub
    Private Sub Set_清單1()
        PA1 = 群組碼_tb.Text : Set_DGV1載入前設定() : SQL_合約試算分表_物料清單一() : Set_DG1載入後設定()
    End Sub
    Private Sub Set_DG1載入後設定()
        CmdSet_For_DGV() : da.Fill(ds4) : 物料清單_dgv.DataSource = ds4.Tables(0) : conn.Close()
        物料清單_dgv.Columns(0).Visible = False : 物料清單_dgv.Columns(1).FillWeight = 100 : 物料清單_dgv.Columns(2).FillWeight = 175 : 物料清單_dgv.Columns(3).Visible = False
        物料清單_dgv.Columns(4).FillWeight = 45
        物料清單_dgv.Columns(0).ReadOnly = True : 物料清單_dgv.Columns(1).ReadOnly = True : 物料清單_dgv.Columns(2).ReadOnly = True
        For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1 : 物料清單_dgv(0, i).Value = i + 1 : Next
        If 低網速模式 = False Then
            物料清單_dgv.Columns(5).FillWeight = 50 : 物料清單_dgv.Columns(5).Visible = True
            Dim mycol As DataGridViewImageColumn = 物料清單_dgv.Columns("圖檔") : mycol.ImageLayout = DataGridViewImageCellLayout.Zoom
        Else
            If 物料清單_dgv.Columns.Count = 6 Then : 物料清單_dgv.Columns(5).Visible = False : End If
        End If
    End Sub
    Private Sub 群組碼_cb_語轉_下拉表單資料載入()
        If 主項目_cb.SelectedIndex = 0 Then : PA49 = "" : ElseIf 主項目_cb.SelectedIndex = 1 Then : PA49 = 物料主項(0)
        ElseIf 主項目_cb.SelectedIndex = 2 Then : PA49 = 物料主項(1) : ElseIf 主項目_cb.SelectedIndex = 3 Then : PA49 = 物料主項(2)
        ElseIf 主項目_cb.SelectedIndex = 4 Then : PA49 = 物料主項(3) : ElseIf 主項目_cb.SelectedIndex = 5 Then : PA49 = 物料主項(4)
        ElseIf 主項目_cb.SelectedIndex = 6 Then : PA49 = 物料主項(5) : ElseIf 主項目_cb.SelectedIndex = 6 Then : PA49 = 物料主項(6)
        ElseIf 主項目_cb.SelectedIndex = 6 Then : PA49 = 物料主項(7) : ElseIf 主項目_cb.SelectedIndex = 6 Then : PA49 = 物料主項(8)
        ElseIf 主項目_cb.SelectedIndex = 6 Then : PA49 = 物料主項(9) : Else : PA49 = "%%" : End If
        SQL_群組碼_語轉_下拉清單1() : 新群組1_cb.Items.Clear() : While (dr.Read()) : 新群組1_cb.Items.Add(dr("群組")) : End While : conn.Close()
        新群組1_cb.Text = "" : 料號1_tb.Text = "" : 物料清單_dgv.DataSource = Nothing : 物料規格_dgv.DataSource = Nothing
    End Sub
    Private Sub 群組編碼_cb_下拉表單資料載入()
        SQL_群組碼_下拉清單() : 群組編碼_cb.Items.Clear() : While (dr.Read()) : 群組編碼_cb.Items.Add(dr("群組碼")) : End While : conn.Close()
    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, "刪除BOM表")
    End Sub
    Private Sub 新增資料2_bt_MouseEnter(sender As Object, e As EventArgs) Handles 新增資料2_bt.MouseEnter
        ToolTip1.SetToolTip(Me.新增資料2_bt, "新增空白資料")
    End Sub
    Private Sub 排序2_bt_MouseEnter(sender As Object, e As EventArgs) Handles 排序2_bt.MouseEnter
        ToolTip1.SetToolTip(Me.排序2_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 總表選擇刪除1_bt_MouseEnter(sender As Object, e As EventArgs) Handles 總表選擇刪除1_bt.MouseEnter
        ToolTip1.SetToolTip(Me.總表選擇刪除1_bt, "刪除")
    End Sub
    Private Sub 工程項目BOM表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FH_ERP_SYS.WindowState = 2 : Me.MdiParent = FH_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        物料清單_dgv.BringToFront() : Me.KeyPreview = True : Panel2.SendToBack() : ToolTip1.ToolTipTitle = "功能說明"
        物料群組1_P.SendToBack() : 物料群組1_P.Visible = False
        BOM表控制1_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        BOM表控制_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        BOM表明細_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        物料清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        物料規格_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        BOM表明細1_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
    End Sub
    Private Sub 工程項目BOM表_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        主項目_cb.SelectedIndex = 1 : SQL_群組碼_語轉_下拉清單1()
        群組編碼_cb_下拉表單資料載入() : 群組碼_cb_語轉_下拉表單資料載入() : 新群組1_cb.SelectedIndex = 0 : 群組碼_tb.Text = 新群組1_cb.Text
        載入群組碼()
        群組碼_cb.SelectedIndex = 0 : 群組碼中_cb.SelectedIndex = 0 : 群組碼印_cb.SelectedIndex = 0
        群組碼1_cb.SelectedIndex = 0 : 群組碼中1_cb.SelectedIndex = 0 : 群組碼印1_cb.SelectedIndex = 0
        Set_BOM表控制() : Set_BOM表控制1() : Set_清單1()
    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 BOM表明細_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles BOM表明細_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = BOM表明細_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If BOM表明細_dgv("項1", e.RowIndex).Value.ToString <> BOM表明細_dgv("項1", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(BOM表明細_dgv.RowHeadersVisible, BOM表明細_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + BOM表明細_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - BOM表明細_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
        For i As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
            If BOM表明細_dgv.Rows(i).Cells("項1").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項1").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項2").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項2").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項3").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項4").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項5").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 : End If
            If BOM表明細_dgv.Rows(i).Cells("項6").Value.ToString = "" Then : BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 : End If

            If BOM表明細_dgv.Rows(i).Cells("項2").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And
                BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Red
            ElseIf BOM表明細_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And
                BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Peru
            ElseIf BOM表明細_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Green
            ElseIf BOM表明細_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Blue
            ElseIf BOM表明細_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black
            Else : BOM表明細_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black : End If
        Next
    End Sub
    Private Sub 物料清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 物料清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            PA = 物料清單_dgv(1, e.RowIndex).Value.ToString
            PA1 = 物料清單_dgv.Rows(e.RowIndex).Cells("群組碼").Value : PA2 = 物料清單_dgv.Rows(e.RowIndex).Cells("料號原則").Value
            SQL_物料圖庫_圖檔讀取()
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("圖檔"), Byte())
                Dim oStream As New MemoryStream(bytes)
                物料圖1_pb.Image = Bitmap.FromStream(oStream)
            End While : conn.Close() : 物料圖1_pb.SizeMode = 4
            '------------------------------------------------------------------------------------------------------------------------------------------
            SQL_建築物料請購單申請_物料規格讀取()
            If dr.Read() Then : KKK2 = dr("規格庫編號") : PA3 = dr("規格庫抬頭指定") : PA4 = dr("群組碼") : End If : conn.Close()
            '------------------------------------------------------------------------------------------------------------------------------------------
            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 ds3 As New DataSet
            物料規格_dgv.DataSource = Nothing : ds3.Clear()
            物料規格_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            物料規格_dgv.ColumnHeadersHeight = 25 : 物料規格_dgv.AllowUserToAddRows = False
            If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then : 物料規格_dgv.RowTemplate.Height = 65
            ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then : 物料規格_dgv.RowTemplate.Height = 45
            ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then : 物料規格_dgv.RowTemplate.Height = 20
            End If
            If KKK2 = "SQLB03" Then : SQL_物料規格_SQLB03() : ElseIf KKK2 = "SQLB04" Then : SQL_物料規格_SQLB04() : ElseIf KKK2 = "SQLB05" Then : SQL_物料規格_SQLB05()
            ElseIf KKK2 = "SQLB06" Then : SQL_物料規格_SQLB06() : ElseIf KKK2 = "SQLB07" Then : SQL_物料規格_SQLB07() : ElseIf KKK2 = "SQLB08" Then : SQL_物料規格_SQLB08() : End If
            da.Fill(ds3) : 物料規格_dgv.DataSource = ds3.Tables(0) : conn.Close() : 料號1_tb.Text = ""

            物料規格_dgv.Columns(0).Visible = False : 物料規格_dgv.Columns(1).Visible = False : 物料規格_dgv.Columns(3).Visible = False
            物料規格_dgv.Columns(2).Width = 17 : 物料規格_dgv.Columns(12).Visible = False : 物料規格_dgv.Columns(13).Visible = False : 物料規格_dgv.Columns(14).Visible = False
            物料規格_dgv.Columns(15).Visible = False
            If KKK2 = "SQLB03" Then
                物料規格_dgv.Columns(7).Visible = False : 物料規格_dgv.Columns(8).Visible = False : 物料規格_dgv.Columns(9).Visible = False : 物料規格_dgv.Columns(10).Visible = False
                物料規格_dgv.Columns(11).Visible = False
                物料規格_dgv.Columns(4).FillWeight = 213 : 物料規格_dgv.Columns(5).FillWeight = 213 : 物料規格_dgv.Columns(6).FillWeight = 214
            ElseIf KKK2 = "SQLB04" Then
                物料規格_dgv.Columns(7).Visible = True : 物料規格_dgv.Columns(8).Visible = False : 物料規格_dgv.Columns(9).Visible = False : 物料規格_dgv.Columns(10).Visible = False
                物料規格_dgv.Columns(11).Visible = False
                物料規格_dgv.Columns(4).FillWeight = 160 : 物料規格_dgv.Columns(5).FillWeight = 160 : 物料規格_dgv.Columns(6).FillWeight = 160 : 物料規格_dgv.Columns(7).FillWeight = 160
            ElseIf KKK2 = "SQLB05" Then
                物料規格_dgv.Columns(7).Visible = True : 物料規格_dgv.Columns(8).Visible = True : 物料規格_dgv.Columns(9).Visible = False : 物料規格_dgv.Columns(10).Visible = False
                物料規格_dgv.Columns(11).Visible = False
                物料規格_dgv.Columns(4).FillWeight = 128 : 物料規格_dgv.Columns(5).FillWeight = 128 : 物料規格_dgv.Columns(6).FillWeight = 128 : 物料規格_dgv.Columns(7).FillWeight = 128
                物料規格_dgv.Columns(8).FillWeight = 128
            ElseIf KKK2 = "SQLB06" Then
                物料規格_dgv.Columns(7).Visible = True : 物料規格_dgv.Columns(8).Visible = True : 物料規格_dgv.Columns(9).Visible = True : 物料規格_dgv.Columns(10).Visible = False
                物料規格_dgv.Columns(11).Visible = False
                物料規格_dgv.Columns(4).FillWeight = 106 : 物料規格_dgv.Columns(5).FillWeight = 107 : 物料規格_dgv.Columns(6).FillWeight = 106 : 物料規格_dgv.Columns(7).FillWeight = 107
                物料規格_dgv.Columns(8).FillWeight = 106 : 物料規格_dgv.Columns(9).FillWeight = 108
            ElseIf KKK2 = "SQLB07" Then
                物料規格_dgv.Columns(7).Visible = True : 物料規格_dgv.Columns(8).Visible = True : 物料規格_dgv.Columns(9).Visible = True : 物料規格_dgv.Columns(10).Visible = True
                物料規格_dgv.Columns(11).Visible = False
                物料規格_dgv.Columns(4).FillWeight = 91 : 物料規格_dgv.Columns(5).FillWeight = 92 : 物料規格_dgv.Columns(6).FillWeight = 91 : 物料規格_dgv.Columns(7).FillWeight = 92
                物料規格_dgv.Columns(8).FillWeight = 91 : 物料規格_dgv.Columns(9).FillWeight = 92 : 物料規格_dgv.Columns(10).FillWeight = 91
            ElseIf KKK2 = "SQLB08" Then
                物料規格_dgv.Columns(7).Visible = True : 物料規格_dgv.Columns(8).Visible = True : 物料規格_dgv.Columns(9).Visible = True : 物料規格_dgv.Columns(10).Visible = True
                物料規格_dgv.Columns(11).Visible = True
                物料規格_dgv.Columns(4).FillWeight = 80 : 物料規格_dgv.Columns(5).FillWeight = 80 : 物料規格_dgv.Columns(6).FillWeight = 80 : 物料規格_dgv.Columns(7).FillWeight = 80
                物料規格_dgv.Columns(8).FillWeight = 80 : 物料規格_dgv.Columns(9).FillWeight = 80 : 物料規格_dgv.Columns(10).FillWeight = 80 : 物料規格_dgv.Columns(11).FillWeight = 80
            End If
            物料規格_dgv.EditMode = DataGridViewEditMode.EditOnEnter
            For i As Integer = 0 To 11 : 物料規格_dgv.Columns(i).ReadOnly = True : Next

            For i As Integer = 0 To 物料規格_dgv.Rows.Count - 1
                物料規格_dgv.Rows(i).Cells("No").Value = i + 1 : 物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value = 0
                If Not IsNumeric(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) Then : MsgBox("欄位輸入必須為數值!!")
                ElseIf Val(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) > 0 Then : 物料規格_dgv.Rows(i).Cells("Pilih").Value = True
                ElseIf Val(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) <= 0 Then : 物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value = "0"
                    物料規格_dgv.Rows(i).Cells("Pilih").Value = False
                End If
            Next i
            If 查詢_tb.Text <> "" Then : lastFoundRowIndex = -1 : lastFoundColIndex = -1 : 查詢DGV中的關鍵字_自動(物料規格_dgv, 查詢_tb) : End If
        End If
    End Sub
    Private Sub BOM表控制1_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles BOM表控制1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            合約編號_tb.Text = BOM表控制1_dgv.Rows(e.RowIndex).Cells("項目").Value.ToString : 項目流水1_tb.Text = BOM表控制1_dgv.Rows(e.RowIndex).Cells("流水號").Value.ToString
            PA = 項目流水1_tb.Text : Set_合約報價明細表()
        End If
    End Sub
    Private Sub 報價明細表_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles BOM表明細1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            物料圖1_pb.Image = Nothing
            If BOM表明細1_dgv.Rows(e.RowIndex).Cells("料號原則").Value.ToString = "" Then : Else
                PA2 = BOM表明細1_dgv.Rows(e.RowIndex).Cells("料號原則").Value.ToString : SQL_物料圖庫_圖檔讀取一()
                While dr.Read() = True
                    Dim unused As Byte() = New Byte(-1) {}
                    Dim bytes As Byte() = DirectCast(dr.Item("圖檔"), Byte())
                    Dim oStream As New MemoryStream(bytes)
                    物料圖1_pb.Image = Bitmap.FromStream(oStream)
                End While : conn.Close() : 物料圖1_pb.SizeMode = 4
            End If
        End If
    End Sub
    Private Sub 報價明細表_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles BOM表明細1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = BOM表明細1_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If BOM表明細1_dgv("項1", e.RowIndex).Value.ToString <> BOM表明細1_dgv("項1", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(BOM表明細1_dgv.RowHeadersVisible, BOM表明細1_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + BOM表明細1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - BOM表明細1_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
        For i As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1
            If BOM表明細1_dgv.Rows(i).Cells("項1").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項1").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項2").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項2").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項3").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項4").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項5").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 : End If
            If BOM表明細1_dgv.Rows(i).Cells("項6").Value.ToString = "" Then : BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 : End If

            If BOM表明細1_dgv.Rows(i).Cells("項2").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And
                BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Red
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項3").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And
                BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Peru
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項4").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Green
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項5").Value = 0 And BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Blue
            ElseIf BOM表明細1_dgv.Rows(i).Cells("項6").Value = 0 Then
                BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black
            Else : BOM表明細1_dgv.Rows(i).DefaultCellStyle.ForeColor = Color.Black : End If
        Next
    End Sub
    Private Sub 物料規格_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 物料規格_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            料號1_tb.Text = 物料規格_dgv.Rows(e.RowIndex).Cells("Nomor_Bagian").Value.ToString : 料號1_tb.Text = 物料規格_dgv.Rows(e.RowIndex).Cells("Nomor_Bagian").Value.ToString
        End If
    End Sub
    Private Sub 物料規格_dgv_CellContentCl(sender As Object, e As DataGridViewCellEventArgs) Handles 物料規格_dgv.CellEnter
        For i As Integer = 0 To 物料規格_dgv.Rows.Count - 1
            If Not IsNumeric(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) Then : MsgBox("欄位輸入必須為數值!!")
                物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value = "0" : 物料規格_dgv.Rows(i).Cells("Pilih").Value = False
            ElseIf Val(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) > 0 Then : 物料規格_dgv.Rows(i).Cells("Pilih").Value = True
            ElseIf Val(物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value) <= 0 Then : 物料規格_dgv.Rows(i).Cells("申請數 Terapkan").Value = "0"
                物料規格_dgv.Rows(i).Cells("Pilih").Value = False
            End If
        Next i
    End Sub
    Private Sub BOM表控制_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles BOM表控制_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            主項目_tb.Text = BOM表控制_dgv.Rows(e.RowIndex).Cells("項目").Value.ToString : 項目流水_tb.Text = BOM表控制_dgv.Rows(e.RowIndex).Cells("流水號").Value.ToString
            PA = 項目流水_tb.Text : Set_BOM表明細() : 項1_tb.Text = "1" : 項2_tb.Text = "0" : 項3_tb.Text = "0" : 項4_tb.Text = "0" : 項5_tb.Text = "0" : 項6_tb.Text = "0"
        End If
    End Sub
    Private Sub BOM表明細_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles BOM表明細_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            項目_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項目").Value.ToString : 料號_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("料號").Value.ToString
            申請數_nud.Value = BOM表明細_dgv.Rows(e.RowIndex).Cells("數量").Value.ToString : 選擇項1_tb.Text = e.RowIndex
            選擇項2_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("排序").Value.ToString() : 明細流水_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("明細流水").Value.ToString()
            項1_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項1").Value.ToString() : 項2_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項2").Value.ToString()
            項3_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項3").Value.ToString() : 項4_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項4").Value.ToString()
            項5_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項5").Value.ToString() : 項6_tb.Text = BOM表明細_dgv.Rows(e.RowIndex).Cells("項6").Value.ToString()
            物料圖1_pb.Image = Nothing
            If BOM表明細_dgv.Rows(e.RowIndex).Cells("料號原則").Value.ToString = "" Then : Else
                PA2 = BOM表明細_dgv.Rows(e.RowIndex).Cells("料號原則").Value.ToString : SQL_物料圖庫_圖檔讀取一()
                While dr.Read() = True
                    Dim unused As Byte() = New Byte(-1) {}
                    Dim bytes As Byte() = DirectCast(dr.Item("圖檔"), Byte())
                    Dim oStream As New MemoryStream(bytes)
                    物料圖1_pb.Image = Bitmap.FromStream(oStream)
                End While : conn.Close() : 物料圖1_pb.SizeMode = 4
            End If
        End If
    End Sub
    Private Sub 群組碼中1_cb_Click(sender As Object, e As EventArgs) Handles 群組碼中1_cb.Click
        CB選擇清單(群組碼中1_cb, 群組碼中1_cb.Text)
        群組碼印1_cb.SelectedIndex = 群組碼中1_cb.SelectedIndex : 群組碼1_cb.SelectedIndex = 群組碼中1_cb.SelectedIndex : Set_BOM表控制1()
    End Sub
    Private Sub 新群組1_cb_Click(sender As Object, e As EventArgs) Handles 新群組1_cb.Click
        CB選擇清單(新群組1_cb) : PA = 新群組1_cb.Text : SQL_群組碼_語轉_選擇() : If dr.Read Then : 群組編碼_cb.Text = dr("群組碼").ToString : End If : PA = ""
    End Sub
    Private Sub 群組編碼_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 群組編碼_cb.SelectedIndexChanged
        群組碼_tb.Text = 群組編碼_cb.Text : Set_清單1()
    End Sub
    Private Sub 搜尋_bt_Click(sender As Object, e As EventArgs) Handles 搜尋_bt.Click
        PA1 = InputBox("請輸入關鍵字")
        If PA1 = "" Then : Else : 查詢_tb.Text = PA1
            Set_DGV1載入前設定() : SQL_建築物料請購單申請_物料清單三() : Set_DG1載入後設定() : 料號1_tb.Text = "" : 精準 = False
        End If
    End Sub
    Private Sub 小間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距_ch.Click
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False
    End Sub
    Private Sub 中間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False
    End Sub
    Private Sub 大間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = False : 大間距_ch.Checked = True
    End Sub
    Private Sub 縮放2_bt_Click(sender As Object, e As EventArgs)
        Dim NX, NY As Integer : NX = BOM表控制1_dgv.Size.Width : NY = BOM表控制1_dgv.Size.Height
        If 放大 = True Then
            BOM表控制1_dgv.Size = New System.Drawing.Point(NX - 400, NY) : 放大 = False : BOM表控制1_dgv.Columns("專案名稱").Visible = False
        Else
            BOM表控制1_dgv.Size = New System.Drawing.Point(NX + 400, NY) : 放大 = True : BOM表控制1_dgv.Columns("專案名稱").Visible = True
        End If
    End Sub
    Private Sub 排序2_bt_Click(sender As Object, e As EventArgs) Handles 排序2_bt.Click
        If BOM表明細_dgv.Rows.Count > 0 Then
            If BOM表明細_dgv.Columns(0).Visible = False Then : BOM表明細_dgv.Columns(0).Visible = True : Else : BOM表明細_dgv.Columns(0).Visible = False : End If
        End If
    End Sub
    Private Sub 明細流水()
        SQL_BOM表明細流水號查詢1()
        If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("明細流水").ToString, 8)) : Else : EDR = 0 : End If : conn.Close() : EDR += 1
        If EDR < 10 Then : ESTR = "BOW0000000" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "BOW000000" & EDR
        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "BOW00000" & EDR : ElseIf EDR > 9999 And EDR < 10000 Then : ESTR = "BOW0000" & EDR
        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "BOW000" & EDR : ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "BOW00" & EDR
        ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "BOW0" & EDR : ElseIf EDR <= 10000000 Then : ESTR = "BOW" & EDR : End If : PA7 = ESTR
    End Sub
    Private Sub 新建_bt_Click(sender As Object, e As EventArgs) Handles 新建_bt.Click
        If 手工建立_ch.Checked = True Then
            PA1 = InputBox("請輸入BOM表主項目名稱") : 主項目_tb.Text = PA1 : 新建主項() : Set_BOM表控制()
            項1_tb.Text = "1" : 項2_tb.Text = "0" : 項3_tb.Text = "0" : 項4_tb.Text = "0" : 項5_tb.Text = "0" : 項6_tb.Text = "0"
        Else
            If 物料群組1_P.Visible = False Then : 物料群組1_P.Visible = True : 物料群組1_P.BringToFront()
                物料群組1_P.Location = New System.Drawing.Point((SplitContainer1.Panel2.Size.Width - 物料群組1_P.Size.Width) / 2,
                                                                (SplitContainer1.Panel2.Size.Height - 物料群組1_P.Size.Height) / 2)
                說明_tb.Text = 說明_tb.Text & vbCrLf & "系統已啟動，等待人員指定BOM表檔案......"
            Else : 物料群組1_P.Visible = False : End If
        End If
    End Sub
    Private Sub 全部選擇_bt_Click(sender As Object, e As EventArgs) Handles 全部選擇_bt.Click
        For I As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1 : BOM表明細1_dgv.Rows(I).Cells("選擇").Value = True : Next
    End Sub
    Private Sub 全部取消_bt_Click(sender As Object, e As EventArgs) Handles 全部取消_bt.Click
        For I As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1 : BOM表明細1_dgv.Rows(I).Cells("選擇").Value = False : Next
    End Sub
    Private Sub 新增資料1_bt_Click(sender As Object, e As EventArgs) Handles 新增資料1_bt.Click
        If 項目流水_tb.Text = "" Then : MsgBox("沒有選擇項目!!") : Else : 明細表存檔()
            For I As Integer = 0 To BOM表明細1_dgv.Rows.Count - 1
                If BOM表明細1_dgv.Rows(I).Cells("選擇").Value = True Then
                    PA = 項目流水_tb.Text : PA2 = BOM表明細1_dgv.Rows(I).Cells("項目").Value.ToString : PA3 = BOM表明細1_dgv.Rows(I).Cells("料號").Value.ToString
                    PA4 = BOM表明細1_dgv.Rows(I).Cells("數量").Value.ToString : PA5 = BOM表明細1_dgv.Rows(I).Cells("單位").Value.ToString : PA6 = "B99" : 明細流水()
                    PA8 = 項1_tb.Text : PA9 = 項2_tb.Text : PA10 = 項3_tb.Text : PA11 = 項4_tb.Text : PA12 = 項5_tb.Text : PA13 = 項6_tb.Text : SQL_BOM表明細_新增()
                End If
            Next : PA = 項目流水_tb.Text : Set_BOM表明細()
            For I As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
                If I < 10 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B0" & I
                ElseIf I > 9 And I < 100 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B" & I
                End If
            Next : 明細表存檔() : MsgBox("資料插入完成!!")
        End If
    End Sub
    Private Sub 關鍵字_bt_Click(sender As Object, e As EventArgs) Handles 關鍵字_bt.Click
        If PA1 = "" Then : Else
            Set_DGV1載入前設定() : SQL_建築物料請購單申請_物料清單三() : Set_DG1載入後設定() : 料號1_tb.Text = ""
        End If
    End Sub
    Private Sub 查詢_bt_Click(sender As Object, e As EventArgs) Handles 查詢_bt.Click
        查詢DGV中的關鍵字_手動(物料規格_dgv, 查詢_tb)
    End Sub
    Private Sub 手工建立_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 手工建立_ch.Click
        手工建立_ch.Checked = True : 檔案導入_ch.Checked = False
    End Sub
    Private Sub 檔案導入_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 檔案導入_ch.Click
        手工建立_ch.Checked = False : 檔案導入_ch.Checked = True
    End Sub
    Private Sub 市構件_bt_Click(sender As Object, e As EventArgs) Handles 市構件_bt.Click
        市構件分類.ShowDialog()
    End Sub
    Private Sub 刪除專案_bt_Click(sender As Object, e As EventArgs) Handles 刪除專案_bt.Click
        If 項目流水_tb.Text <> "" Then
            Dim result As DialogResult = MessageBox.Show("否要刪除這筆資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = DialogResult.Yes Then
                PA = InputBox("確定刪除資料請輸入 [YES] !!")
                If PA = "YES" Then
                    PA = 項目流水_tb.Text : SQL_BOM表控制_刪除() : SQL_BOM表明細_刪除_全部()
                    主項目_tb.Text = "" : 項目流水_tb.Text = "" : BOM表明細_dgv.DataSource = Nothing : Set_BOM表控制()
                End If
            End If
        Else : MsgBox("請先選擇要刪除的專案!!") : End If
    End Sub
    Private Sub 總表選擇刪除1_bt_Click(sender As Object, e As EventArgs) Handles 總表選擇刪除1_bt.Click
        If 明細流水_tb.Text <> "" Then
            Dim result As DialogResult = MessageBox.Show("否要刪除這筆資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = DialogResult.Yes Then
                PA7 = 明細流水_tb.Text : SQL_BOM表明細_刪除() : PA = 項目流水_tb.Text : Set_BOM表明細()
            End If
        Else : MsgBox("請先選擇要刪除的物料/資料!!") : End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 主項目_tb.Text <> "" And 項目流水_tb.Text <> "" Then
            PA40 = 群組碼中_cb.Text : SQL_工程項目BOM_群組碼_查詢() : If dr.Read Then : PA40 = dr("群組碼").ToString : End If
            PA1 = 主項目_tb.Text : PA = 項目流水_tb.Text : SQL_BOM表控制_存檔() : 明細表存檔() : MsgBox("存檔完成!!") : Set_BOM表控制() : Set_BOM表明細()
        End If
    End Sub
    Private Sub 主項目_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 主項目_cb.SelectedIndexChanged
        群組碼_cb_語轉_下拉表單資料載入()
    End Sub
    Private Sub 明細表存檔()
        PA = 項目流水_tb.Text
        For i As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
            PA1 = BOM表明細_dgv.Rows(i).Cells("排序").Value.ToString : PA2 = BOM表明細_dgv.Rows(i).Cells("項目").Value.ToString
            PA3 = BOM表明細_dgv.Rows(i).Cells("料號").Value.ToString : PA4 = BOM表明細_dgv.Rows(i).Cells("數量").Value.ToString
            PA5 = BOM表明細_dgv.Rows(i).Cells("單位").Value.ToString : PA7 = BOM表明細_dgv.Rows(i).Cells("明細流水").Value.ToString
            PA8 = BOM表明細_dgv.Rows(i).Cells("項1").Value.ToString : PA9 = BOM表明細_dgv.Rows(i).Cells("項2").Value.ToString
            PA10 = BOM表明細_dgv.Rows(i).Cells("項3").Value.ToString : PA11 = BOM表明細_dgv.Rows(i).Cells("項4").Value.ToString
            PA12 = BOM表明細_dgv.Rows(i).Cells("項5").Value.ToString : PA13 = BOM表明細_dgv.Rows(i).Cells("項6").Value.ToString : SQL_BOM表明細_存檔()
        Next
    End Sub
    Private Sub 群組設定_bt_Click(sender As Object, e As EventArgs) Handles 群組設定_bt.Click
        專案項目BOM表_群組設定.ShowDialog() : 專案項目BOM表_群組設定.BringToFront() : 載入群組碼()
    End Sub
    Private Sub 群組碼中_cb_Click(sender As Object, e As EventArgs) Handles 群組碼中_cb.Click
        CB選擇清單(群組碼中_cb, 群組碼中_cb.Text)
        群組碼印_cb.SelectedIndex = 群組碼中_cb.SelectedIndex : 群組碼_cb.SelectedIndex = 群組碼中_cb.SelectedIndex : Set_BOM表控制()
    End Sub
    Private Sub 新增資料2_bt_Click(sender As Object, e As EventArgs) Handles 新增資料2_bt.Click
        If 項目流水_tb.Text = "" Then : MsgBox("沒有選擇項目!!") : Else : 明細表存檔()
            PA20 = InputBox("請輸入要新增幾行空白欄未!! (請輸入數字)?")
            If IsNumeric(PA20) = False Then : MsgBox("你輸入的不是數字!!") : Else
                For ia As Integer = 1 To PA20
                    PA = 項目流水_tb.Text : PA2 = "" : PA3 = "" : PA4 = "0" : PA5 = "" : 明細流水()
                    If 選擇項1_tb.Text = "" Then
                        SQL_BOM表明細流水號查詢()
                        If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("排序").ToString, 2)) : Else : EDR = 0 : End If : conn.Close() : EDR += 1
                        If EDR < 10 Then : ESTR = "B0" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "B" & EDR : End If : PA6 = ESTR
                    Else : PA6 = 選擇項2_tb.Text & "-1" : End If
                    PA8 = 項1_tb.Text : PA9 = 項2_tb.Text : PA10 = 項3_tb.Text : PA11 = 項4_tb.Text : PA12 = 項5_tb.Text : PA13 = 項6_tb.Text
                    SQL_BOM表明細_新增()
                Next : PA = 項目流水_tb.Text : Set_BOM表明細()
                For I As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
                    If I < 10 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B0" & I
                    ElseIf I > 9 And I < 100 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B" & I
                    End If
                Next : 明細表存檔()
            End If
        End If
    End Sub
    Private Sub 選擇材料新增到明細表_bt_Click(sender As Object, e As EventArgs) Handles 選擇材料新增到明細表_bt.Click
        If 項目流水_tb.Text = "" Then : MsgBox("沒有選擇合約!!") : Else
            If 料號1_tb.Text = "" Then : MsgBox("沒有選擇物料!!") : Else
                明細表存檔()
                PA = 項目流水_tb.Text : PA2 = "" : PA3 = 料號1_tb.Text : PA4 = "0" : PA5 = "" : 明細流水()
                If 選擇項1_tb.Text = "" Then
                    SQL_BOM表明細流水號查詢()
                    If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("排序").ToString, 2)) : Else : EDR = 0 : End If : conn.Close() : EDR += 1
                    If EDR < 10 Then : ESTR = "B0" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "B" & EDR : End If : PA6 = ESTR
                Else : PA6 = 選擇項2_tb.Text & "-1" : End If
                PA8 = 項1_tb.Text : PA9 = 項2_tb.Text : PA10 = 項3_tb.Text : PA11 = 項4_tb.Text : PA12 = 項5_tb.Text : PA13 = 項6_tb.Text
                SQL_BOM表明細_新增()
                PA = 項目流水_tb.Text : Set_BOM表明細()
                For I As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
                    If I < 10 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B0" & I
                    ElseIf I > 9 And I < 100 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B" & I
                    End If
                Next : 明細表存檔()
            End If
        End If
    End Sub
    Private Sub 修改明細表上的材料指定_bt_Click(sender As Object, e As EventArgs) Handles 修改明細表上的材料指定_bt.Click
        If 項目流水_tb.Text = "" Then : MsgBox("請先選擇要修改的資料是哪一筆!!") : Else
            If 料號1_tb.Text = "" Then : MsgBox("沒有選擇物料!!") : Else
                明細表存檔() : PA7 = 明細流水_tb.Text : PA3 = 料號1_tb.Text : SQL_BOM表明細_存檔_料號() : Set_BOM表明細()
            End If
        End If
    End Sub
    Private Sub 詳細資料2_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 詳細資料2_ch.Click
        Set_BOM表明細()
    End Sub
    Private Sub Cancel_bt_Click(sender As Object, e As EventArgs) Handles Cancel_bt.Click
        If 物料群組1_P.Visible = False Then : 物料群組1_P.Visible = True : 物料群組1_P.BringToFront() : Else : 物料群組1_P.Visible = False : End If
    End Sub
    Private Sub 選路徑1_bt_Click(sender As Object, e As EventArgs) Handles 選路徑1_bt.Click
        If 開啟檔案.ShowDialog <> Windows.Forms.DialogResult.Cancel Then : 路徑1_tb.Text = 開啟檔案.FileName : 不再動作2 = False : 說明_tb.Text = "系統已啟動，等待人員指定BOM表檔案......"
            '----------服務紀錄總表-----------------------------------------------------------------------------------------------------------------------------------------------
            file_name = 路徑1_tb.Text : xlApp = CreateObject("Excel.Application") : xlApp.DisplayAlerts = False : xlApp.Visible = False : xlApp.Workbooks.Open(file_name)
            xlSheet = xlApp.Worksheets(1) : xlSheet.Activate()
            文件判斷 = xlSheet.Cells(2, 1).value : 文件判斷 &= xlSheet.Cells(2, 2).value : 文件判斷 &= xlSheet.Cells(2, 3).value : 文件判斷 &= xlSheet.Cells(2, 4).value
            文件判斷 &= xlSheet.Cells(2, 5).value : 文件判斷 &= xlSheet.Cells(2, 6).value : 文件判斷 &= xlSheet.Cells(2, 7).value : 文件判斷 &= xlSheet.Cells(2, 8).value
            文件判斷 &= xlSheet.Cells(2, 9).value : 文件判斷 &= xlSheet.Cells(2, 10).value : 文件判斷 &= xlSheet.Cells(2, 11).value : 文件判斷 &= xlSheet.Cells(2, 12).value
            If 文件判斷 = "項次編號零件名稱品名規格數量單位加工類別材質表處熱處理供應廠商Remarks" Then
                '---先判斷該BOM表是否已經存在----------------------------------------------------------------
                主項目2_tb.Text = xlSheet.Cells(1, 1).value : PA1 = xlSheet.Cells(1, 1).value
                SQL_BOM表控制_項目查詢()
                If dr.Read Then : MsgBox("主項目名稱重複，無法存檔!!") : 說明_tb.Text = 說明_tb.Text & vbCrLf & "程式運算結束....."
                    xlApp.Visible = True : xlApp.Quit() : xlApp = Nothing : Exit Sub
                End If : 說明_tb.Text = 說明_tb.Text & vbCrLf & "開始導入資料....."
                '---比對料號---------------------------------------------------------------------------------
                說明_tb.Text = 說明_tb.Text & vbCrLf & "已啟動第一階段，開始比對EXCEL表中的物料資料系統中是否都底完善......"
                For i = 3 To 99999
                    PA2 = xlSheet.Cells(i, 2).value : If PA2 = "" Then : Exit For : End If   '------遇空白結束--------------------
                    SQL_物料規格表_料號查詢1()
                    If dr.Read Then : Else
                        '------------如果沒有查到，先比對是否有物料編碼------------------------------------------------------------
                        If Len(PA2) = 8 Then : PA = Strings.Left(xlSheet.Cells(i, 2).value, 4) : Else : PA = Strings.Left(xlSheet.Cells(i, 2).value, 18) : End If
                        SQL_物料資料庫_物料編碼庫重複確認()
                        If dr.Read() Then
                            '------------新增物料------------------------------------------------------------
                            PA3 = dr("群組碼").ToString : PA4 = dr("料號原則").ToString : PA50 = xlSheet.Cells(i, 2).value
                            If Len(PA4) = 4 Then
                                PA5 = xlSheet.Cells(i, 3).value : PA6 = xlSheet.Cells(i, 8).value : PA7 = xlSheet.Cells(i, 9).value : PA8 = xlSheet.Cells(i, 10).value
                                PA9 = xlSheet.Cells(i, 4).value : PA10 = xlSheet.Cells(i, 11).value : PA11 = "" : PA12 = ""
                            Else
                                If Strings.Right(PA4, 3) = "000" Then : PA5 = "" : PA6 = "" : PA7 = "" : PA8 = "" : PA9 = "" : PA10 = "" : PA11 = "" : PA12 = ""
                                Else : PA5 = xlSheet.Cells(i, 8).value : PA6 = xlSheet.Cells(i, 9).value : PA7 = xlSheet.Cells(i, 10).value : End If
                            End If : SQL_物料規格表_新增()
                            PA30 = "自動新增物料 " & xlSheet.Cells(i, 2).value & " [" & xlSheet.Cells(i, 3).value & "]" : 說明_tb.Text = 說明_tb.Text & vbCrLf & PA30
                        Else
                            If Len(xlSheet.Cells(i, 2).value) = 8 Then : PA20 = Strings.Left(xlSheet.Cells(i, 2).value, 2) & "%"
                            Else : PA20 = xlSheet.Cells(i, 7).value & "-" & Strings.Left(xlSheet.Cells(i, 2).value, 9) & "%" : End If
                            SQL_物料圖庫_群組圖片版本1()
                            If dr.Read Then
                                '------------新增物料群組------------------------------------------------------------
                                PA1 = dr("群組碼").ToString
                                If Len(xlSheet.Cells(i, 2).value) = 8 Then : PA2 = Strings.Left(xlSheet.Cells(i, 2).value, 4) : PA = PA2
                                Else : PA2 = Strings.Left(xlSheet.Cells(i, 2).value, 18) : PA = PA2 : End If : PA4 = xlSheet.Cells(i, 6).value
                                If PA4 = "" Then : PA4 = "PCS" : End If
                                PA3 = xlSheet.Cells(i, 3).value : SQL_物料資料庫_物料編碼庫新增() : SQL_物料資料庫_物料編碼庫語轉重複確認()
                                If dr.Read() Then : Else
                                    PA5 = xlSheet.Cells(i, 3).value : PA11 = "繁體中文" : SQL_物料資料庫_物料編碼庫新增_語轉4()
                                    PA11 = "English" : SQL_物料資料庫_物料編碼庫新增_語轉4() : PA11 = "Indonesian" : SQL_物料資料庫_物料編碼庫新增_語轉4()
                                    SQL_物料資料庫_物料圖片流水號最後一筆()
                                    If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("圖片流水號").ToString, 8)) : Else : EDR = 0 : End If
                                    conn.Close() : EDR += 1
                                    If EDR < 10 Then : ESTR = "PG" & "0000000" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PG" & "000000" & EDR
                                    ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PG" & "00000" & EDR : ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PG" & "0000" & EDR
                                    ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PG" & "000" & EDR : ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PG" & "00" & EDR
                                    ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PG" & "0" & EDR : ElseIf EDR > 9999999 Then : ESTR = "PG" & EDR : End If
                                    PA20 = ESTR : PA21 = PA1 : PA22 = PA2
                                    PA49 = Str + "\LOGG.jpg" : SQL_物料資料庫_圖檔入系統1() : conn.Close()
                                    '------------新增抬頭指定------------------------------------------------------------
                                    PA6 = xlSheet.Cells(i, 3).value : PA7 = PA1 : PA8 = PA
                                    If Len(xlSheet.Cells(i, 2).value) = 8 Then : PA9 = "SQLB06" : PA10 = "HE06-03" : Else
                                        If Strings.Right(Strings.Left(xlSheet.Cells(i, 2).value, 18), 3) = "000" Then
                                            PA9 = "SQLB03" : PA10 = "HE03-01" : Else : PA9 = "SQLB03" : PA10 = "HE03-02"
                                        End If
                                    End If
                                    PA30 = "自動新增原則 " & PA2 & " [" & xlSheet.Cells(i, 3).value & "]" : 說明_tb.Text = 說明_tb.Text & vbCrLf & PA30 : 說明_tb.ScrollToCaret()
                                    ConnOpen() : SQL_物料資料庫_物料資訊控制表新增() : CmdSet_For_dr()
                                    '------------新增物料------------------------------------------------------------
                                    PA2 = xlSheet.Cells(i, 2).value
                                    SQL_物料資料庫_物料編碼庫重複確認()
                                    If dr.Read() Then
                                        PA3 = dr("群組碼").ToString : PA4 = dr("料號原則").ToString : PA50 = xlSheet.Cells(i, 2).value
                                        If Len(PA4) = 4 Then
                                            PA5 = xlSheet.Cells(i, 3).value : PA6 = xlSheet.Cells(i, 8).value : PA7 = xlSheet.Cells(i, 9).value : PA8 = xlSheet.Cells(i, 10).value
                                            PA9 = xlSheet.Cells(i, 4).value : PA10 = xlSheet.Cells(i, 11).value : PA11 = "" : PA12 = ""
                                        Else
                                            If Strings.Right(PA4, 3) = "000" Then : PA5 = "" : PA6 = "" : PA7 = "" : PA8 = "" : PA9 = "" : PA10 = "" : PA11 = "" : PA12 = ""
                                            Else : PA5 = xlSheet.Cells(i, 8).value : PA6 = xlSheet.Cells(i, 9).value : PA7 = xlSheet.Cells(i, 10).value : End If
                                        End If : SQL_物料規格表_新增()
                                        PA30 = "自動新增物料 " & xlSheet.Cells(i, 2).value & " [" & xlSheet.Cells(i, 3).value & "]" : 說明_tb.Text = 說明_tb.Text & vbCrLf & PA30
                                        說明_tb.ScrollToCaret()
                                    End If
                                End If
                            Else
                                '--------如果沒有匹配到任何條件，將單元格底色填充為紅色----------------------------------------------
                                xlSheet.Cells(i, 2).Interior.Color = RGB(255, 0, 0) : 不再動作2 = True
                                If Len(xlSheet.Cells(i, 2).value) = 8 Then : PA20 = Strings.Left(xlSheet.Cells(i, 2).value, 2) & " (請到物料資料庫做確認)"
                                Else : PA20 = xlSheet.Cells(i, 7).value & "-" & Strings.Left(xlSheet.Cells(i, 2).value, 9) & "系列" : End If
                                說明_tb.Text = 說明_tb.Text & vbCrLf & "請先至物料資料庫新增群組 [" & PA20 & "]"
                                說明_tb.ScrollToCaret()
                            End If
                        End If
                    End If
                Next
                If 不存入系統_ch.Checked = False Then
                    If 不再動作2 = True Then : MsgBox("文件中有物料無法被判斷，所以無法自動加入，請重新確認物料群組邏輯!!!") : Else
                        說明_tb.Text = 說明_tb.Text & vbCrLf & "已啟動第二階段，開始將EXCEL表中的資料些入BOM表......"
                        說明_tb.ScrollToCaret()
                        PA1 = 主項目2_tb.Text : 主項目_tb.Text = PA1 : 新建主項() : Set_BOM表控制()
                        項1_tb.Text = "1" : 項2_tb.Text = "0" : 項3_tb.Text = "0" : 項4_tb.Text = "0" : 項5_tb.Text = "0" : 項6_tb.Text = "0"
                        For i = 3 To 99999
                            PA2 = xlSheet.Cells(i, 3).value : If PA2 = "" Then : Exit For : End If   '------遇空白結束--------------------
                            PA = 項目流水_tb.Text : PA2 = "" : PA3 = xlSheet.Cells(i, 2).value : PA4 = xlSheet.Cells(i, 5).value : PA5 = "" : 明細流水()
                            If 選擇項1_tb.Text = "" Then
                                SQL_BOM表明細流水號查詢()
                                If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("排序").ToString, 2)) : Else : EDR = 0 : End If : conn.Close() : EDR += 1
                                If EDR < 10 Then : ESTR = "B0" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "B" & EDR : End If : PA6 = ESTR
                            Else : PA6 = 選擇項2_tb.Text & "-1" : End If
                            ParseDataToTextBoxes(xlSheet.Cells(i, 1).value)
                            If 項1_tb.Text = "" Then : 項1_tb.Text = "0" : End If : If 項2_tb.Text = "" Then : 項2_tb.Text = "0" : End If
                            If 項3_tb.Text = "" Then : 項3_tb.Text = "0" : End If : If 項4_tb.Text = "" Then : 項4_tb.Text = "0" : End If
                            If 項5_tb.Text = "" Then : 項5_tb.Text = "0" : End If : If 項6_tb.Text = "" Then : 項6_tb.Text = "0" : End If
                            PA8 = 項1_tb.Text : PA9 = 項2_tb.Text : PA10 = 項3_tb.Text : PA11 = 項4_tb.Text : PA12 = 項5_tb.Text : PA13 = 項6_tb.Text
                            SQL_BOM表明細_新增()
                        Next
                        PA = 項目流水_tb.Text : Set_BOM表明細()
                        For I As Integer = 0 To BOM表明細_dgv.Rows.Count - 1
                            If I < 10 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B0" & I
                            ElseIf I > 9 And I < 100 Then : BOM表明細_dgv.Rows(I).Cells("排序").Value = "B" & I
                            End If
                        Next : 明細表存檔()
                        MsgBox("BOM表資料導入系統完成!!!") : 說明_tb.Text = 說明_tb.Text & vbCrLf & "BOM表資料導入系統完成!!!" : 說明_tb.ScrollToCaret()
                    End If
                Else : MsgBox("BOM表資料導入系統完成!!!") : 說明_tb.Text = 說明_tb.Text & vbCrLf & "物料表資料導入系統完成!!!" : 說明_tb.ScrollToCaret() : End If
            Else : MsgBox("BOM表檔案格式不吻合!!") : 路徑1_tb.Text = "" : End If
            'xlApp.Visible = True : xlApp.Quit() : xlApp = Nothing
        End If
    End Sub
    Public Sub ParseDataToTextBoxes(input As String)
        Dim parts As String() = input.Split(New Char() {"."c, "-"c}, StringSplitOptions.RemoveEmptyEntries) : ClearAllTextBoxes()
        For i As Integer = 0 To Math.Min(parts.Length - 1, 5) : Dim number As Integer
            If Integer.TryParse(parts(i), number) Then : If number >= 1 AndAlso number <= 99 Then : SetTextBoxValue(i + 1, number.ToString()) : End If : End If
        Next
    End Sub
    Private Sub ClearAllTextBoxes()
        For i As Integer = 1 To 6 : SetTextBoxValue(i, String.Empty) : Next
    End Sub
    Private Sub SetTextBoxValue(index As Integer, value As String)
        Select Case index
            Case 1 : 項1_tb.Text = value : Case 2 : 項2_tb.Text = value : Case 3 : 項3_tb.Text = value
            Case 4 : 項4_tb.Text = value : Case 5 : 項5_tb.Text = value : Case 6 : 項6_tb.Text = value
        End Select
    End Sub
    Private Sub 新建主項()
        SQL_BOM表控制_項目查詢()
        If dr.Read Then : MsgBox("主項目名稱重複，無法存檔!!") : 不再動作1 = True : Exit Sub : Else : 不再動作1 = False : End If
        SQL_BOM表控制流水號查詢()
        If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("流水號").ToString, 8)) : Else : EDR = 0 : End If : conn.Close() : EDR += 1
        If EDR < 10 Then : ESTR = "BOM0000000" & EDR : ElseIf EDR > 9 And EDR < 100 Then : ESTR = "BOM000000" & EDR
        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "BOM00000" & EDR : ElseIf EDR > 9999 And EDR < 10000 Then : ESTR = "BOM0000" & EDR
        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "BOM000" & EDR : ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "BOM00" & EDR
        ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "BOM0" & EDR : ElseIf EDR <= 10000000 Then : ESTR = "BOM" & EDR : End If : PA = ESTR
        PA40 = "" : 專案項目BOM表_選擇群組.ShowDialog() : 專案項目BOM表_選擇群組.BringToFront()
        If PA40 = "" Then : MsgBox("沒有選擇群組!!") : Else : SQL_BOM表控制_新增() : 項目流水_tb.Text = PA
            Dim index As Integer = 群組碼_cb.Items.IndexOf(PA40)
            If index <> -1 Then : 群組碼_cb.SelectedIndex = index : 群組碼中_cb.SelectedIndex = index : 群組碼印_cb.SelectedIndex = index : End If
            PA40 = ""
        End If
    End Sub
    '----------------------自創視窗滑鼠拖曳功能---------------------------------------------------------------------------------------------------------
    Dim OldX, OldY As Long
    Dim drag As Boolean
    Private Sub 物料群組1_Panel_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 物料群組1_P.MouseDown
        If e.Button = MouseButtons.Left Then : OldX = e.X : OldY = e.Y : drag = True : End If
    End Sub
    Private Sub 物料群組1_Panel_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 物料群組1_P.MouseMove
        If drag Then : 物料群組1_P.Left = 物料群組1_P.Left + e.X - OldX : 物料群組1_P.Top = 物料群組1_P.Top + e.Y - OldY : End If
    End Sub
    Private Sub 物料群組1_Panel_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 物料群組1_P.MouseUp
        drag = False
    End Sub
End Class