Imports System.IO

Public Class 案件摘要表
    Dim DGVX As Integer
    Private Sub 計算分流1_Tick(sender As Object, e As EventArgs) Handles 計算分流1.Tick
        計算分流1.Enabled = False
        Dim ds1 As New DataSet
        案件附件_dgv.DataSource = Nothing : ds1.Clear()
        案件附件_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        案件附件_dgv.ColumnHeadersHeight = 25 : 案件附件_dgv.AllowUserToAddRows = False : 案件附件_dgv.RowTemplate.Height = 25
        SQL_連線字串_圖片資料庫() : SQL_案件摘要附件()
        da.Fill(ds1) : 案件附件_dgv.DataSource = ds1.Tables(0) : conn.Close()
        案件附件_dgv.Columns(0).Visible = False : 案件附件_dgv.Columns(1).ReadOnly = True
        For i As Integer = 0 To 案件附件_dgv.Rows.Count - 1 : 案件附件_dgv.Rows(i).Cells(1).Value = "附件-" & i + 1 : Next
        If 案件附件_dgv.Rows.Count > 0 Then : DGVX = 0 : 附件編碼1_lb.Text = 案件附件_dgv.Rows(0).Cells(0).Value.ToString : 圖片清單讀取() : End If
    End Sub
    Private Sub 計算分流2_Tick(sender As Object, e As EventArgs) Handles 計算分流2.Tick
        計算分流2.Enabled = False
        Dim ds1 As New DataSet
        附件_dgv.DataSource = Nothing : ds1.Clear()
        附件_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        附件_dgv.ColumnHeadersHeight = 25 : 附件_dgv.AllowUserToAddRows = False : 附件_dgv.RowTemplate.Height = 25
        SQL_連線字串_圖片資料庫() : SQL_案件承租附件()
        da.Fill(ds1) : 附件_dgv.DataSource = ds1.Tables(0) : conn.Close()
        附件_dgv.Columns(0).Visible = False : 附件_dgv.Columns(1).ReadOnly = True
        For i As Integer = 0 To 附件_dgv.Rows.Count - 1 : 附件_dgv.Rows(i).Cells(1).Value = "附件-" & i + 1 : Next
        If 附件_dgv.Rows.Count > 0 Then : DGVX = 0 : 附件編碼_lb.Text = 附件_dgv.Rows(0).Cells(0).Value.ToString : 圖片清單讀取1() : End If
    End Sub
    Private Sub Set_客戶清單()
        Dim ds1 As New DataSet
        清單2_dgv.DataSource = Nothing : ds1.Clear()
        清單2_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單2_dgv.ColumnHeadersHeight = 25 : 清單2_dgv.AllowUserToAddRows = False : 清單2_dgv.RowTemplate.Height = 35
        PA40 = 關鍵字1_tb.Text : PA41 = 分類2_cb.Text : SQL_客戶資料表讀取()
        da.Fill(ds1) : 清單2_dgv.DataSource = ds1.Tables(0) : conn.Close()
        清單2_dgv.Columns(0).Visible = False : 清單2_dgv.Columns(1).ReadOnly = True
        For i As Integer = 2 To 14 : 清單2_dgv.Columns(i).Visible = False : Next
    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 : 清單1_dgv.RowTemplate.Height = 35
        PA40 = 關鍵字_tb.Text : PA41 = 分類1_cb.Text : SQL_案件摘要讀取()
        da.Fill(ds1) : 清單1_dgv.DataSource = ds1.Tables(0) : conn.Close()
        清單1_dgv.Columns(0).Visible = False : 清單1_dgv.Columns(1).ReadOnly = True
        For i As Integer = 2 To 24 : 清單1_dgv.Columns(i).Visible = False : 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 : 土地_dgv.RowTemplate.Height = 25
        SQL_案件土地清單()
        da.Fill(ds1) : 土地_dgv.DataSource = ds1.Tables(0) : conn.Close()
        土地_dgv.Columns(0).Visible = False : 土地_dgv.Columns(1).Visible = False : 土地_dgv.Columns(5).ReadOnly = True
        土地_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        土地_dgv.Columns("土地面積(平米)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        土地_dgv.Columns("坪數").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        For i As Integer = 6 To 17 : 土地_dgv.Columns(i).Visible = False : 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 : 區塊_dgv.RowTemplate.Height = 25
        SQL_案件建坪清單()
        da.Fill(ds1) : 區塊_dgv.DataSource = ds1.Tables(0) : conn.Close()
        區塊_dgv.Columns(0).Visible = False : 區塊_dgv.Columns(1).Visible = False ': 區塊_dgv.Columns(4).ReadOnly = True
        區塊_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        區塊_dgv.Columns("坪數").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        區塊_dgv.Columns("面寬(米)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        區塊_dgv.Columns("深度(米)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        區塊計算()
        SQL_建坪類別() : 區塊_cb.Items.Clear() : 區塊_cb.Items.Add("") : While (dr.Read()) : 區塊_cb.Items.Add(dr("建坪編號")) : End While : conn.Close()
    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 : 承租_dgv.RowTemplate.Height = 35
        SQL_案件承租讀取()
        da.Fill(ds1) : 承租_dgv.DataSource = ds1.Tables(0) : conn.Close()
        For i As Integer = 0 To 22 : 承租_dgv.Columns(i).Visible = False : Next : 承租_dgv.Columns(23).ReadOnly = True
        For i As Integer = 0 To 承租_dgv.Rows.Count - 1
            承租_dgv.Rows(i).Cells("承租").Value = 承租_dgv.Rows(i).Cells("建坪編號").Value.ToString & "-" & 承租_dgv.Rows(i).Cells("客戶名稱").Value.ToString
        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 : 太陽_dgv.RowTemplate.Height = 25
        SQL_案件太陽讀取()
        da.Fill(ds1) : 太陽_dgv.DataSource = ds1.Tables(0) : conn.Close()
        For i As Integer = 0 To 6 : 太陽_dgv.Columns(i).Visible = False : Next : 太陽_dgv.Columns(1).Visible = True : 太陽_dgv.Columns(1).ReadOnly = True
        For i As Integer = 0 To 太陽_dgv.Rows.Count - 1 : 太陽_dgv.Rows(i).Cells(1).Value = "檔案-" & i + 1 : 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 區塊刪除_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 刪除附件1_bt_MouseEnter(sender As Object, e As EventArgs) Handles 刪除附件1_bt.MouseEnter
        ToolTip1.SetToolTip(Me.刪除附件1_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 列印1_bt_MouseEnter(sender As Object, e As EventArgs) Handles 列印1_bt.MouseEnter
        ToolTip1.SetToolTip(Me.列印1_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 = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        地址_tb.ReadOnly = True : 附件編碼1_lb.Text = "" : 視窗2_pl.Visible = False : 承租客_tb.BringToFront() : 承租區塊編號_lb.Text = ""
        區塊_dgv.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
        Label56.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        總坪數1_tb.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
    End Sub
    Private Sub 案件摘要表_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        SQL_客戶類別() : 分類2_cb.Items.Clear() : 分類2_cb.Items.Add("") : While (dr.Read()) : 分類2_cb.Items.Add(dr("類別")) : End While : conn.Close()
        SQL_案件類別() : 分類1_cb.Items.Clear() : 分類1_cb.Items.Add("") : While (dr.Read()) : 分類1_cb.Items.Add(dr("使用用途")) : End While : conn.Close()
        Set_案件清單()
    End Sub
    Private Sub 客戶資料表_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        If 地址_tb.Text = "" Then : SQL_案件摘要刪除() : End If
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 清單1_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 清單1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            流水號_tb.Text = 清單1_dgv.Rows(e.RowIndex).Cells("單據編號").Value.ToString : 地址_tb.Text = 清單1_dgv.Rows(e.RowIndex).Cells("地址/地號").Value.ToString
            聯絡人_tb.Text = 清單1_dgv.Rows(e.RowIndex).Cells("聯絡人").Value.ToString : 製錶_tb.Text = 清單1_dgv.Rows(e.RowIndex).Cells("製錶").Value.ToString


            圖片庫 = 清單1_dgv.Rows(e.RowIndex).Cells("圖片庫").Value.ToString
            建坪編號_tb.Text = "" : 土地編號_tb.Text = "" : 承租編號_tb.Text = "" : 附件編碼1_lb.Text = "" : 承租區塊編號_lb.Text = "" : 附件編碼_lb.Text = ""
            承租狀態_cb.Text = "" : 區塊_cb.Text = "" : 承租客_tb.Text = "" : 客戶流水_tb.Text = "" : 聯絡人1_tb.Text = "" : 電話1_tb.Text = ""
            聯絡人2_tb.Text = "" : 電話2_tb.Text = "" : 聯絡人3_tb.Text = "" : 電話3_tb.Text = "" : 開發窗口_tb.Text = "" : 店長_tb.Text = ""
            租期開始1_dtp.CustomFormat = " " : 租期結束1_dtp.CustomFormat = " " : 免租開始1_dtp.CustomFormat = " " : 免租結束1_dtp.CustomFormat = " "
            租金_nud.Value = 0 : 押金_nud.Value = 0 : 備註1_tb.Text = "" : 附件_dgv.DataSource = Nothing : 附件_pb.Image = Nothing
            附件1_pb.Image = Nothing : Set_土地清單() : Set_區塊清單() : 計算分流1.Enabled = True : Set_承租清單() : Set_太陽清單()
            If 土地_dgv.Rows.Count > 0 Then : DGVX = 0 : 土地_dgv點擊() : Else
                用途_cb.Text = "" : 備註_tb.Text = "" : 土地租期_nup.Value = 0 : 租期開始_dtp.CustomFormat = " " : 租期結束_dtp.CustomFormat = " "
                土地租金_nud.Value = 0 : 月坪租金_nud.Value = 0 : 保證月_nud.Value = 0 : 保證金_nud.Value = 0 : 免租_nud.Value = 0 : 地主_tb.Text = ""
                免租開始_dtp.CustomFormat = " " : 免租結束_dtp.CustomFormat = " " : 執照_dtp.CustomFormat = " " : 建照_dtp.CustomFormat = " "
            End If
            If 太陽_dgv.Rows.Count > 0 Then : DGVX = 0 : 太陽_dgv點擊() : Else
                太_出租_tb.Text = "" : 太_承租_tb.Text = "" : KW數_tb.Text = "" : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.CustomFormat = " "
                太_租金比_tb.Text = ""
            End If
        End If
    End Sub
    Private Sub 太陽_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 太陽_dgv.CellClick
        If e.RowIndex = -1 Then : Else : DGVX = e.RowIndex : 太陽_dgv點擊() : End If
    End Sub
    Private Sub 太陽_dgv點擊()
        綠能編號_tb.Text = 太陽_dgv.Rows(DGVX).Cells("流水號").Value.ToString
        太_出租_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_出租人").Value.ToString : 太_承租_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_承租人").Value.ToString
        KW數_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_KW數").Value.ToString : 太_租金比_tb.Text = 太陽_dgv.Rows(DGVX).Cells("太陽_租金%").Value.ToString
        If 太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString = "" Then : 太陽開始_dtp.CustomFormat = " " : Else
            If Strings.Left(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 5) = "  -  " Then : 太陽開始_dtp.CustomFormat = " " : Else
                太陽開始_dtp.CustomFormat = "yyyy/MM/dd" : 太陽開始_dtp.Value = Strings.Left(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 10)
            End If
        End If
        If 太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString = "" Then : 太陽結束_dtp.CustomFormat = " " : Else
            If Strings.Right(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 5) = "  -  " Then : 太陽結束_dtp.CustomFormat = " " : Else
                太陽結束_dtp.CustomFormat = "yyyy/MM/dd" : 太陽結束_dtp.Value = Strings.Right(太陽_dgv.Rows(DGVX).Cells("太陽_租期").Value.ToString, 10)
            End If
        End If
    End Sub
    Private Sub 土地_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 土地_dgv.CellClick
        If e.RowIndex = -1 Then : Else : DGVX = e.RowIndex : 土地_dgv點擊() : End If
    End Sub
    Private Sub 土地_dgv點擊()
        土地編號_tb.Text = 土地_dgv.Rows(DGVX).Cells("流水號").Value.ToString : 地主_tb.Text = 土地_dgv.Rows(DGVX).Cells("地主").Value.ToString
        SQL_案件類別() : 用途_cb.Items.Clear() : 用途_cb.Items.Add("") : While (dr.Read()) : 用途_cb.Items.Add(dr("使用用途")) : End While : conn.Close()
        用途_cb.Text = 土地_dgv.Rows(DGVX).Cells("使用用途").Value.ToString : 備註_tb.Text = 土地_dgv.Rows(DGVX).Cells("備註").Value.ToString
        土地租期_nup.Text = 土地_dgv.Rows(DGVX).Cells("土地租期").Value.ToString
        If 土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString = "" Then : 租期開始_dtp.CustomFormat = " " : Else
            If Strings.Left(土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString, 5) = "  -  " Then : 租期開始_dtp.CustomFormat = " " : Else
                租期開始_dtp.CustomFormat = "yyyy/MM/dd" : 租期開始_dtp.Value = Strings.Left(土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString, 10)
            End If
        End If
        If 土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString = "" Then : 租期結束_dtp.CustomFormat = " " : Else
            If Strings.Right(土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString, 5) = "  -  " Then : 租期結束_dtp.CustomFormat = " " : Else
                租期結束_dtp.CustomFormat = "yyyy/MM/dd" : 租期結束_dtp.Value = Strings.Right(土地_dgv.Rows(DGVX).Cells("租期備註").Value.ToString, 10)
            End If
        End If
        If 土地_dgv.Rows(DGVX).Cells("土地月租").Value.ToString = "" Then : 土地租金_nud.Value = 0 : Else
            土地租金_nud.Value = 土地_dgv.Rows(DGVX).Cells("土地月租").Value.ToString
        End If
        If 土地_dgv.Rows(DGVX).Cells("每坪月租").Value.ToString = "" Then : 月坪租金_nud.Value = 0 : Else
            月坪租金_nud.Value = 土地_dgv.Rows(DGVX).Cells("每坪月租").Value.ToString
        End If
        If 土地_dgv.Rows(DGVX).Cells("保證月").Value.ToString = "" Then : 保證月_nud.Value = 0 : Else
            保證月_nud.Value = 土地_dgv.Rows(DGVX).Cells("保證月").Value.ToString
        End If
        If 土地_dgv.Rows(DGVX).Cells("押金/保證金").Value.ToString = "" Then : 保證金_nud.Value = 0 : Else
            保證金_nud.Value = 土地_dgv.Rows(DGVX).Cells("押金/保證金").Value.ToString
        End If
        If 土地_dgv.Rows(DGVX).Cells("免租興建期").Value.ToString = "" Then : 免租_nud.Value = 0 : Else
            免租_nud.Value = 土地_dgv.Rows(DGVX).Cells("免租興建期").Value.ToString
        End If
        If 土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString = "" Then : 免租開始_dtp.CustomFormat = " " : Else
            If Strings.Left(土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString, 5) = "  -  " Then : 免租開始_dtp.CustomFormat = " " : Else
                免租開始_dtp.CustomFormat = "yyyy/MM/dd" : 免租開始_dtp.Value = Strings.Left(土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString, 10)
            End If
        End If
        If 土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString = "" Then : 免租結束_dtp.CustomFormat = " " : Else
            If Strings.Right(土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString, 5) = "  -  " Then : 免租結束_dtp.CustomFormat = " " : Else
                免租結束_dtp.CustomFormat = "yyyy/MM/dd" : 免租結束_dtp.Value = Strings.Right(土地_dgv.Rows(DGVX).Cells("建期備註").Value.ToString, 10)
            End If
        End If
        If 土地_dgv.Rows(DGVX).Cells("取得使用執照日").Value.ToString = "" Then : 執照_dtp.CustomFormat = " " : Else
            If 土地_dgv.Rows(DGVX).Cells("取得使用執照日").Value.ToString = " " Then : 執照_dtp.CustomFormat = " " : Else
                執照_dtp.CustomFormat = "yyyy/MM/dd" : 執照_dtp.Value = 土地_dgv.Rows(DGVX).Cells("取得使用執照日").Value.ToString
            End If
        End If
        If 土地_dgv.Rows(DGVX).Cells("建照核准日").Value.ToString = "" Then : 建照_dtp.CustomFormat = " " : Else
            If 土地_dgv.Rows(DGVX).Cells("建照核准日").Value.ToString = " " Then : 建照_dtp.CustomFormat = " " : Else
                建照_dtp.CustomFormat = "yyyy/MM/dd" : 建照_dtp.Value = 土地_dgv.Rows(DGVX).Cells("建照核准日").Value.ToString
            End If
        End If
    End Sub
    Private Sub 土地_dgv_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 土地_dgv.CellEndEdit
        土地計算()
    End Sub
    Private Sub 土地計算()
        Dim NS1, NS2 As Double
        For i As Integer = 0 To 土地_dgv.Rows.Count - 1
            If IsNumeric(土地_dgv.Rows(i).Cells("土地面積(平米)").Value.ToString.Replace(",", "")) = False Then : 土地_dgv.Rows(i).Cells("土地面積(平米)").Value = 0 : End If
            NS1 += CDbl(土地_dgv.Rows(i).Cells("土地面積(平米)").Value.ToString) : 土地_dgv.Rows(i).Cells("坪數").Value = CDbl(土地_dgv.Rows(i).Cells("土地面積(平米)").Value.ToString) / 3.3058
            NS2 += CDbl(土地_dgv.Rows(i).Cells("坪數").Value.ToString)
            土地_dgv.Rows(i).Cells("土地面積(平米)").Value = Strings.Format(CDbl(土地_dgv.Rows(i).Cells("土地面積(平米)").Value.ToString), "#,##0.00")
            土地_dgv.Rows(i).Cells("坪數").Value = Strings.Format(CDbl(土地_dgv.Rows(i).Cells("坪數").Value.ToString), "#,##0.00")
        Next : 總平米_tb.Text = Strings.Format(NS1, "#,##0.00") : 總坪數_tb.Text = Strings.Format(NS2, "#,##0.00")
    End Sub
    Private Sub 區塊_dgv_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 區塊_dgv.CellEndEdit
        區塊計算()
    End Sub
    Private Sub 區塊計算()
        Dim NS1 As Double
        For i As Integer = 0 To 區塊_dgv.Rows.Count - 1
            If IsNumeric(區塊_dgv.Rows(i).Cells("坪數").Value.ToString.Replace(",", "")) = False Then : 區塊_dgv.Rows(i).Cells("坪數").Value = 0 : End If
            If IsNumeric(區塊_dgv.Rows(i).Cells("面寬(米)").Value.ToString.Replace(",", "")) = False Then : 區塊_dgv.Rows(i).Cells("面寬(米)").Value = 0 : End If
            If IsNumeric(區塊_dgv.Rows(i).Cells("深度(米)").Value.ToString.Replace(",", "")) = False Then : 區塊_dgv.Rows(i).Cells("深度(米)").Value = 0 : End If
            NS1 += CDbl(區塊_dgv.Rows(i).Cells("坪數").Value.ToString)
            區塊_dgv.Rows(i).Cells("面寬(米)").Value = Strings.Format(CDbl(區塊_dgv.Rows(i).Cells("面寬(米)").Value.ToString), "#,##0")
            區塊_dgv.Rows(i).Cells("深度(米)").Value = Strings.Format(CDbl(區塊_dgv.Rows(i).Cells("深度(米)").Value.ToString), "#,##0")
            區塊_dgv.Rows(i).Cells("坪數").Value = Strings.Format(CDbl(區塊_dgv.Rows(i).Cells("坪數").Value.ToString), "#,##0.00")
        Next : 總坪數1_tb.Text = Strings.Format(NS1, "#,##0.00")
    End Sub
    Private Sub 案件附件_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 案件附件_dgv.CellClick
        If e.RowIndex = -1 Then : Else : DGVX = e.RowIndex : 圖片清單讀取() : End If
    End Sub
    Private Sub 圖片清單讀取()
        附件編碼1_lb.Text = 案件附件_dgv.Rows(DGVX).Cells(0).Value.ToString : PA2 = 案件附件_dgv.Rows(DGVX).Cells(0).Value.ToString
        SQL_連線字串_圖片資料庫() : 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 Sub
    Private Sub 附件_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 附件_dgv.CellClick
        If e.RowIndex = -1 Then : Else : DGVX = e.RowIndex : 圖片清單讀取1() : End If
    End Sub
    Private Sub 圖片清單讀取1()
        附件編碼1_lb.Text = 附件_dgv.Rows(DGVX).Cells(0).Value.ToString : PA2 = 附件_dgv.Rows(DGVX).Cells(0).Value.ToString
        SQL_連線字串_圖片資料庫() : 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)
            附件_pb.Image = Bitmap.FromStream(oStream)
        End While : conn.Close() : 附件_pb.SizeMode = 4
    End Sub
    Private Sub 清單2_dgv_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles 清單2_dgv.CellDoubleClick
        If e.RowIndex = -1 Then : Else
            承租客_tb.Text = 清單2_dgv.Rows(e.RowIndex).Cells("客戶名稱").Value.ToString : 客戶流水_tb.Text = 清單2_dgv.Rows(e.RowIndex).Cells("流水號").Value.ToString
            視窗2_pl.Visible = False
        End If
    End Sub
    Private Sub 土地_dgv_SelectionChanged(sender As Object, e As EventArgs) Handles 土地_dgv.SelectionChanged
        If 土地_dgv.SelectedCells.Count > 0 Then
            Dim selectedRow As DataGridViewRow = 土地_dgv.Rows(土地_dgv.SelectedCells(0).RowIndex)
            If selectedRow IsNot Nothing AndAlso Not selectedRow.IsNewRow Then : 土地編號_tb.Text = selectedRow.Cells("流水號").Value.ToString() : End If
        End If
    End Sub
    Private Sub 區塊_dgv_SelectionChanged(sender As Object, e As EventArgs) Handles 區塊_dgv.SelectionChanged
        If 區塊_dgv.SelectedCells.Count > 0 Then
            Dim selectedRow As DataGridViewRow = 區塊_dgv.Rows(區塊_dgv.SelectedCells(0).RowIndex)
            If selectedRow IsNot Nothing AndAlso Not selectedRow.IsNewRow Then : 建坪編號_tb.Text = selectedRow.Cells("流水號").Value.ToString() : End If
        End If
    End Sub
    Private Sub 承租_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 承租_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            承租編號_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("流水號").Value.ToString : 區塊_cb.Text = 承租_dgv.Rows(e.RowIndex).Cells("建坪編號").Value.ToString
            承租狀態_cb.Text = 承租_dgv.Rows(e.RowIndex).Cells("承租狀態").Value.ToString : 承租客_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("客戶名稱").Value.ToString
            客戶流水_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("承租客").Value.ToString : 聯絡人1_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("聯絡人1").Value.ToString
            聯絡人2_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("聯絡人2").Value.ToString : 聯絡人3_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("聯絡人3").Value.ToString
            電話1_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("電話1").Value.ToString : 電話2_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("電話2").Value.ToString
            電話3_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("電話3").Value.ToString : 開發窗口_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("開發窗口").Value.ToString
            店長_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("店長/店經理").Value.ToString : 租金_nud.Value = 承租_dgv.Rows(e.RowIndex).Cells("租金").Value.ToString
            押金_nud.Value = 承租_dgv.Rows(e.RowIndex).Cells("押金/保證金").Value.ToString : 備註1_tb.Text = 承租_dgv.Rows(e.RowIndex).Cells("保證金備註").Value.ToString
            If 承租_dgv.Rows(e.RowIndex).Cells("租期開始").Value.ToString = " " Then : 租期開始1_dtp.CustomFormat = " " : Else
                租期開始1_dtp.CustomFormat = "yyyy/MM/dd" : 租期開始_dtp.Value = Strings.Left(承租_dgv.Rows(e.RowIndex).Cells("租期開始").Value.ToString, 10)
            End If
            If 承租_dgv.Rows(e.RowIndex).Cells("租期結束").Value.ToString = " " Then : 租期結束1_dtp.CustomFormat = " " : Else
                租期結束1_dtp.CustomFormat = "yyyy/MM/dd" : 租期結束1_dtp.Value = Strings.Left(承租_dgv.Rows(e.RowIndex).Cells("租期結束").Value.ToString, 10)
            End If
            If Strings.Left(承租_dgv.Rows(e.RowIndex).Cells("免租期").Value.ToString, 5) = "  -  " Then : 免租開始1_dtp.CustomFormat = " " : Else
                免租開始1_dtp.CustomFormat = "yyyy/MM/dd" : 免租開始1_dtp.Value = Strings.Left(承租_dgv.Rows(e.RowIndex).Cells("免租期").Value.ToString, 10)
            End If
            If Strings.Right(承租_dgv.Rows(e.RowIndex).Cells("免租期").Value.ToString, 5) = "  -  " Then : 免租結束1_dtp.CustomFormat = " " : Else
                免租結束1_dtp.CustomFormat = "yyyy/MM/dd" : 免租結束1_dtp.Value = Strings.Right(承租_dgv.Rows(e.RowIndex).Cells("免租期").Value.ToString, 10)
            End If : 附件編碼_lb.Text = "" : 附件_pb.Image = Nothing : 計算分流2.Enabled = True
        End If
    End Sub
    Private Sub 新增_bt_Click(sender As Object, e As EventArgs) Handles 新增_bt.Click
        Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd")
        SQL_案件摘要流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
        NUM0 += 1
        If NUM0 < 10 Then : 流水號_tb.Text = "YS-" & DTP1 & "-00" & NUM0
        ElseIf NUM0 > 9 And NUM0 < 100 Then : 流水號_tb.Text = "YS-" & DTP1 & "-0" & NUM0
        ElseIf NUM0 > 99 Then : 流水號_tb.Text = "YS-" & DTP1 & "-" & NUM0
        End If : 地址_tb.ReadOnly = False : 清單1_dgv.Enabled = False
        地址_tb.Text = "" : 地主_tb.Text = "" : 用途_cb.Text = "" : 備註_tb.Text = "" : 土地租期_nup.Value = 0
        土地租金_nud.Value = 0 : 月坪租金_nud.Value = 0 : 保證月_nud.Value = 0 : 保證金_nud.Value = 0 : 太_出租_tb.Text = "" : 太_承租_tb.Text = "" : KW數_tb.Text = ""
        太_租金比_tb.Text = "" : 附件1_pb.Image = Nothing : 附件_pb.Image = Nothing : 案件附件_dgv.DataSource = Nothing : 土地_dgv.DataSource = Nothing
        區塊_dgv.DataSource = Nothing : 承租_dgv.DataSource = Nothing : 附件_dgv.DataSource = Nothing : 區塊_cb.Text = "" : 坪數_nud.Value = 0 : 承租客_tb.Text = ""
        聯絡人1_tb.Text = "" : 聯絡人2_tb.Text = "" : 聯絡人2_tb.Text = "" : 電話1_tb.Text = "" : 電話2_tb.Text = "" : 電話3_tb.Text = "" : 開發窗口_tb.Text = ""
        店長_tb.Text = "" : 面積_tb.Text = "" : 租金_nud.Value = 0 : 押金_nud.Value = 0 : 聯絡人_tb.Text = "" : 免租_nud.Value = 0 : 製錶_tb.Text = gUserName
        建照_dtp.Format = DateTimePickerFormat.Custom : 建照_dtp.CustomFormat = " " : 執照_dtp.Format = DateTimePickerFormat.Custom : 執照_dtp.CustomFormat = " "
        太陽開始_dtp.Format = DateTimePickerFormat.Custom : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " "
        免租開始1_dtp.Format = DateTimePickerFormat.Custom : 免租開始1_dtp.CustomFormat = " " : 免租結束1_dtp.Format = DateTimePickerFormat.Custom : 免租結束1_dtp.CustomFormat = " "
        免租開始_dtp.Format = DateTimePickerFormat.Custom : 免租開始_dtp.CustomFormat = " " : 免租結束_dtp.Format = DateTimePickerFormat.Custom : 免租結束_dtp.CustomFormat = " "
        租期開始1_dtp.Format = DateTimePickerFormat.Custom : 租期開始1_dtp.CustomFormat = " " : 租期結束1_dtp.Format = DateTimePickerFormat.Custom : 租期結束1_dtp.CustomFormat = " "
        租期開始_dtp.Format = DateTimePickerFormat.Custom : 租期開始_dtp.CustomFormat = " " : 租期結束_dtp.Format = DateTimePickerFormat.Custom : 租期結束_dtp.CustomFormat = " "
        SQL_資料庫選擇() : If dr.Read() Then : 圖片庫 = dr("文件資料庫").ToString : Else : MsgBox("圖片資料庫已滿，請找系統管理員!!") : Me.Close() : End If
        SQL_案件摘要新增() : Set_案件清單()
    End Sub
    Private Sub 確認新增_bt_Click(sender As Object, e As EventArgs) Handles 確認新增_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            If 地址_tb.Text = "" Then : MsgBox("[地址/地號] 不可以空白!!") : Else
                If 製錶_tb.Text = "" Or 製錶_tb.Text = gUserName Then : 確認新增() : Else : If CC(14) = True Then : 確認新增() : End If : End If
                土地存檔() : 建坪存檔() : SQL_案件摘要太陽修改()

                SQL_建坪類別() : 區塊_cb.Items.Clear() : 區塊_cb.Items.Add("") : While (dr.Read()) : 區塊_cb.Items.Add(dr("建坪編號")) : End While : conn.Close()
                地址_tb.ReadOnly = True : 清單1_dgv.Enabled = True : 承租區塊編號_lb.Text = "" : 附件編碼_lb.Text = ""
                承租狀態_cb.Text = "" : 區塊_cb.Text = "" : 承租客_tb.Text = "" : 客戶流水_tb.Text = "" : 聯絡人1_tb.Text = "" : 電話1_tb.Text = ""
                聯絡人2_tb.Text = "" : 電話2_tb.Text = "" : 聯絡人3_tb.Text = "" : 電話3_tb.Text = "" : 開發窗口_tb.Text = "" : 店長_tb.Text = ""
                租期開始1_dtp.CustomFormat = " " : 租期結束1_dtp.CustomFormat = " " : 免租開始1_dtp.CustomFormat = " " : 免租結束1_dtp.CustomFormat = " "
                租金_nud.Value = 0 : 押金_nud.Value = 0 : 備註1_tb.Text = "" : 附件_dgv.DataSource = Nothing : Set_土地清單() : Set_太陽清單()
            End If
        End If
    End Sub
    Private Sub 確認新增()
        If 清單1_dgv.Enabled = False Then : SQL_案件摘要驗證() : If dr.Read Then : MsgBox("[地址/地號]重複，無法新增[地址/地號]資料!!") : 地址_tb.Text = "" : Exit Sub : End If : End If
        SQL_案件摘要修改() : Set_案件清單() : MsgBox("存檔完成!!")
    End Sub
    Private Sub 土地存檔()
        For i As Integer = 0 To 土地_dgv.Rows.Count - 1
            PA1 = 土地_dgv.Rows(i).Cells("面積編號").Value.ToString : PA2 = 土地_dgv.Rows(i).Cells("土地面積(平米)").Value.ToString
            PA3 = 土地_dgv.Rows(i).Cells("坪數").Value.ToString : PA = 土地_dgv.Rows(i).Cells("流水號").Value.ToString
            PA4 = 土地_dgv.Rows(i).Cells("地主").Value.ToString : SQL_案件土地修改()
        Next
        If 土地編號_tb.Text <> "" Then : SQL_案件土地修改1() : End If
    End Sub
    Private Sub 土地新增_bt_Click(sender As Object, e As EventArgs) Handles 土地新增_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            土地存檔() : Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd")
            SQL_案件土地流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
            NUM0 += 1
            If NUM0 < 10 Then : 土地編號_tb.Text = "YT-" & DTP1 & "-00" & NUM0
            ElseIf NUM0 > 9 And NUM0 < 100 Then : 土地編號_tb.Text = "YT-" & DTP1 & "-0" & NUM0
            ElseIf NUM0 > 99 Then : 土地編號_tb.Text = "YT-" & DTP1 & "-" & NUM0
            End If : SQL_案件土地清單新增() : Set_土地清單()
        End If
    End Sub
    Private Sub 土地刪除_bt_Click(sender As Object, e As EventArgs) Handles 土地刪除_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            If 土地編號_tb.Text = "" Then : MsgBox("請先選擇 [土地資料] 或是 [新增土地資料]，才能執行後續動作!!") : Else
                Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的土地資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = DialogResult.Yes Then
                    PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                    If PA11 = "YES" Then : 土地存檔() : SQL_案件土地刪除() : Set_土地清單() : MsgBox("刪除完成!!") : End If
                End If
            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
            建坪存檔() : Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd")
            SQL_案件建坪流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
            NUM0 += 1
            If NUM0 < 10 Then : 建坪編號_tb.Text = "YS-" & DTP1 & "-00" & NUM0
            ElseIf NUM0 > 9 And NUM0 < 100 Then : 建坪編號_tb.Text = "YS-" & DTP1 & "-0" & NUM0
            ElseIf NUM0 > 99 Then : 建坪編號_tb.Text = "YS-" & DTP1 & "-" & NUM0
            End If : SQL_案件建坪清單新增() : Set_區塊清單()
        End If
    End Sub
    Private Sub 區塊刪除_bt_Click(sender As Object, e As EventArgs) Handles 區塊刪除_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            If 建坪編號_tb.Text = "" Then : MsgBox("請先選擇 [建坪資料] 或是 [新增建坪資料]，才能執行後續動作!!") : Else
                Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的建坪資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = DialogResult.Yes Then
                    PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                    If PA11 = "YES" Then : 建坪存檔() : SQL_案件建坪刪除() : Set_區塊清單() : MsgBox("刪除完成!!") : End If
                End If
            End If
        End If
    End Sub
    Private Sub 建坪存檔()
        For i As Integer = 0 To 區塊_dgv.Rows.Count - 1
            PA1 = 區塊_dgv.Rows(i).Cells("建坪編號").Value.ToString : PA2 = 區塊_dgv.Rows(i).Cells("備註").Value.ToString
            PA3 = 區塊_dgv.Rows(i).Cells("坪數").Value.ToString : PA4 = 區塊_dgv.Rows(i).Cells("面寬(米)").Value.ToString
            PA5 = 區塊_dgv.Rows(i).Cells("深度(米)").Value.ToString : PA = 區塊_dgv.Rows(i).Cells("流水號").Value.ToString : SQL_案件建坪修改()
            Set_承租清單()
        Next
    End Sub
    Private Sub 太陽新增_bt_Click(sender As Object, e As EventArgs) Handles 太陽新增_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd")
            SQL_案件太陽流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
            NUM0 += 1
            If NUM0 < 10 Then : 綠能編號_tb.Text = "SP-" & DTP1 & "-00" & NUM0
            ElseIf NUM0 > 9 And NUM0 < 100 Then : 綠能編號_tb.Text = "SP-" & DTP1 & "-0" & NUM0
            ElseIf NUM0 > 99 Then : 綠能編號_tb.Text = "SP-" & DTP1 & "-" & NUM0
            End If : SQL_案件太陽清單新增() : Set_太陽清單()
        End If
    End Sub
    Private Sub 太陽刪除_bt_Click(sender As Object, e As EventArgs) Handles 太陽刪除_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇 [案件資料] 或是 [新增案件資料]，才能執行後續動作!!") : Else
            If 綠能編號_tb.Text = "" Then : MsgBox("請先選擇 [太陽能承租檔案] 或是 [新增太陽能承租檔案]，才能執行後續動作!!") : Else
                Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的太陽能承租資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = DialogResult.Yes Then
                    PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                    If PA11 = "YES" Then : SQL_案件建坪刪除() : Set_太陽清單() : MsgBox("刪除完成!!") : End If
                End If
            End If
        End If
    End Sub
    Private Sub 讀取資料夾路徑1_bt_Click(sender As Object, e As EventArgs) Handles 讀取資料夾路徑1_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇案件資料，才能繼續後續的操作!!") : Else
            開啟檔案.Filter = "圖片文件 (*.jpg; *.jpeg; *.png; *.bmp)|*.jpg;*.jpeg;*.png;*.bmp"
            開啟檔案.Title = "選擇圖片文件"
            開啟檔案.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            If 開啟檔案.ShowDialog() = DialogResult.OK Then
                Try
                    附件1_pb.Image = Image.FromFile(開啟檔案.FileName) : 附件1_pb.SizeMode = 4

                    Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd") : Dim 附件流水 As String = ""
                    SQL_連線字串_圖片資料庫() : SQL_案件摘要附件流水驗證() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
                    NUM0 += 1
                    If NUM0 < 10 Then : 附件流水 = "IM-" & DTP1 & "-00" & NUM0
                    ElseIf NUM0 > 9 And NUM0 < 100 Then : 附件流水 = "IM-" & DTP1 & "-0" & NUM0
                    ElseIf NUM0 > 99 Then : 附件流水 = "IM-" & DTP1 & "-" & NUM0
                    End If : PA1 = 附件流水 : PA49 = 開啟檔案.FileName : SQL_案件摘要入系統()
                    計算分流1.Enabled = True : MsgBox("附件導入系統完成!!")
                Catch ex As Exception
                    MessageBox.Show("無法加載圖片: " & ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End If
    End Sub
    Private Sub 刪除附件1_bt_Click(sender As Object, e As EventArgs) Handles 刪除附件1_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇案件資料，才能繼續後續的操作!!") : Else
            If 附件編碼1_lb.Text = "" Then : MsgBox("請先選擇附件!!") : Else
                Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的案件附件?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = DialogResult.Yes Then
                    PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                    If PA11 = "YES" Then
                        SQL_連線字串_圖片資料庫() : SQL_案件摘要附件刪除()
                        計算分流1.Enabled = True : MsgBox("刪除完成!!")
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub 區塊_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 區塊_cb.SelectedIndexChanged
        坪數_nud.Value = 0 : 承租區塊編號_lb.Text = "" : 面積_tb.Text = ""
        For i As Integer = 0 To 區塊_dgv.Rows.Count - 1
            If 區塊_cb.Text = 區塊_dgv.Rows(i).Cells("建坪編號").Value.ToString Then
                坪數_nud.Value = CDbl(區塊_dgv.Rows(i).Cells("坪數").Value.ToString)
                承租區塊編號_lb.Text = 區塊_dgv.Rows(i).Cells("流水號").Value.ToString
                面積_tb.Text = 區塊_dgv.Rows(i).Cells("坪數").Value.ToString & " 坪 / 面寬" & 區塊_dgv.Rows(i).Cells("面寬(米)").Value.ToString &
                               " (米) / 深度" & 區塊_dgv.Rows(i).Cells("深度(米)").Value.ToString & " (米)" : Exit Sub
            End If
        Next
    End Sub
    Private Sub 承租新增_bt_Click(sender As Object, e As EventArgs) Handles 承租新增_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇案件資料，才能繼續後續的操作!!") : Else
            If 承租區塊編號_lb.Text = "" Then : MsgBox("請先選擇 [區塊]，才能繼續後續的操作!!") : Else
                If 客戶流水_tb.Text = "" Then : MsgBox("請先選擇 [客戶]，才能繼續後續的操作!!") : Else
                    If 承租狀態_cb.Text = "" Then : MsgBox("請先選擇 [承租狀態]，才能繼續後續的操作!!") : Else
                        Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd")
                        SQL_案件承租流水() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
                        NUM0 += 1
                        If NUM0 < 10 Then : 承租編號_tb.Text = "YD-" & DTP1 & "-00" & NUM0
                        ElseIf NUM0 > 9 And NUM0 < 100 Then : 承租編號_tb.Text = "YD-" & DTP1 & "-0" & NUM0
                        ElseIf NUM0 > 99 Then : 承租編號_tb.Text = "YD-" & DTP1 & "-" & NUM0
                        End If : SQL_案件承租清單新增() : Set_承租清單()
                    End If
                End If
            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 承租區塊編號_lb.Text = "" Then : MsgBox("請先選擇 [區塊]，才能繼續後續的操作!!") : Else
                If 客戶流水_tb.Text = "" Then : MsgBox("請先選擇 [客戶]，才能繼續後續的操作!!") : Else
                    If 承租編號_tb.Text = "" Then : MsgBox("請先選擇 [承租資料] 或是 [新增承租資料]，才能繼續後續的操作!!") : Else
                        If 承租狀態_cb.Text = "" Then : MsgBox("請先選擇 [承租狀態]，才能繼續後續的操作!!") : Else
                            SQL_案件承租清單修改() : Set_承租清單() : MsgBox("存檔完成!!")
                        End If
                    End If
                End If
            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 承租區塊編號_lb.Text = "" Then : MsgBox("請先選擇 [區塊]，才能繼續後續的操作!!") : Else
                If 客戶流水_tb.Text = "" Then : MsgBox("請先選擇 [客戶]，才能繼續後續的操作!!") : Else
                    If 承租編號_tb.Text = "" Then : MsgBox("請先選擇 [承租資料] 或是 [新增承租資料]，才能繼續後續的操作!!") : Else
                        If 承租狀態_cb.Text = "" Then : MsgBox("請先選擇 [承租狀態]，才能繼續後續的操作!!") : Else
                            Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的承租資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                            If result = DialogResult.Yes Then
                                PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                                If PA11 = "YES" Then
                                    SQL_案件承租清單刪除() : Set_承租清單() : MsgBox("刪除完成!!") : 承租區塊編號_lb.Text = "" : 附件編碼_lb.Text = ""
                                    承租狀態_cb.Text = "" : 區塊_cb.Text = "" : 承租客_tb.Text = "" : 客戶流水_tb.Text = "" : 聯絡人1_tb.Text = "" : 電話1_tb.Text = ""
                                    聯絡人2_tb.Text = "" : 電話2_tb.Text = "" : 聯絡人3_tb.Text = "" : 電話3_tb.Text = "" : 開發窗口_tb.Text = "" : 店長_tb.Text = ""
                                    租期開始1_dtp.CustomFormat = " " : 租期結束1_dtp.CustomFormat = " " : 免租開始1_dtp.CustomFormat = " " : 免租結束1_dtp.CustomFormat = " "
                                    租金_nud.Value = 0 : 押金_nud.Value = 0 : 備註1_tb.Text = "" : 附件_dgv.DataSource = Nothing : MsgBox("刪除完成!!")
                                End If
                            End If
                        End If
                    End If
                End If
            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 承租編號_tb.Text = "" Then : MsgBox("請先選擇承租資料，才能繼續後續的操作!!") : Else
                開啟檔案.Filter = "圖片文件 (*.jpg; *.jpeg; *.png; *.bmp)|*.jpg;*.jpeg;*.png;*.bmp"
                開啟檔案.Title = "選擇圖片文件"
                開啟檔案.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
                If 開啟檔案.ShowDialog() = DialogResult.OK Then
                    Try
                        附件1_pb.Image = Image.FromFile(開啟檔案.FileName) : 附件1_pb.SizeMode = 4

                        Dim NUM0 As Integer : DTP1 = Format(Today(), "yyyyMMdd") : Dim 附件流水 As String = ""
                        SQL_連線字串_圖片資料庫() : SQL_案件承租附件流水驗證() : If dr.Read Then : NUM0 = Strings.Right(dr("流水號").ToString, 3) : Else : NUM0 = 0 : End If
                        NUM0 += 1
                        If NUM0 < 10 Then : 附件流水 = "IM-" & DTP1 & "-00" & NUM0
                        ElseIf NUM0 > 9 And NUM0 < 100 Then : 附件流水 = "IM-" & DTP1 & "-0" & NUM0
                        ElseIf NUM0 > 99 Then : 附件流水 = "IM-" & DTP1 & "-" & NUM0
                        End If : PA1 = 附件流水 : PA49 = 開啟檔案.FileName : SQL_案件承租入系統()
                        計算分流2.Enabled = True : MsgBox("附件導入系統完成!!")
                    Catch ex As Exception
                        MessageBox.Show("無法加載圖片: " & ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            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 附件編碼_lb.Text = "" Then : MsgBox("請先選擇附件!!") : Else
                Dim result As DialogResult = MessageBox.Show("請確定是否要刪除選定的案件附件?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = DialogResult.Yes Then
                    PA11 = InputBox("請輸入大寫 [YES]，並按下確認來刪除資料!!")
                    If PA11 = "YES" Then : SQL_連線字串_圖片資料庫() : SQL_案件承租附件刪除() : 計算分流2.Enabled = True : MsgBox("刪除完成!!") : End If
                End If
            End If
        End If
    End Sub
    Private Sub 清除1_bt_Click(sender As Object, e As EventArgs) Handles 清除1_bt.Click
        租期開始_dtp.Format = DateTimePickerFormat.Custom : 租期開始_dtp.CustomFormat = " " : 租期結束_dtp.Format = DateTimePickerFormat.Custom : 租期結束_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除2_bt_Click(sender As Object, e As EventArgs) Handles 清除2_bt.Click
        免租開始_dtp.Format = DateTimePickerFormat.Custom : 免租開始_dtp.CustomFormat = " " : 免租結束_dtp.Format = DateTimePickerFormat.Custom : 免租結束_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除3_bt_Click(sender As Object, e As EventArgs) Handles 清除3_bt.Click
        建照_dtp.Format = DateTimePickerFormat.Custom : 建照_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除4_bt_Click(sender As Object, e As EventArgs) Handles 清除4_bt.Click
        執照_dtp.Format = DateTimePickerFormat.Custom : 執照_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除5_bt_Click(sender As Object, e As EventArgs) Handles 清除5_bt.Click
        太陽開始_dtp.Format = DateTimePickerFormat.Custom : 太陽開始_dtp.CustomFormat = " " : 太陽結束_dtp.Format = DateTimePickerFormat.Custom : 太陽結束_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除6_bt_Click(sender As Object, e As EventArgs) Handles 清除6_bt.Click
        租期開始1_dtp.Format = DateTimePickerFormat.Custom : 租期開始1_dtp.CustomFormat = " " : 租期結束1_dtp.Format = DateTimePickerFormat.Custom : 租期結束1_dtp.CustomFormat = " "
    End Sub
    Private Sub 清除7_bt_Click(sender As Object, e As EventArgs) Handles 清除7_bt.Click
        免租開始1_dtp.Format = DateTimePickerFormat.Custom : 免租開始1_dtp.CustomFormat = " " : 免租結束1_dtp.Format = DateTimePickerFormat.Custom : 免租結束1_dtp.CustomFormat = " "
    End Sub
    Private Sub 建照_dtp_MouseDown(sender As Object, e As EventArgs) Handles 建照_dtp.MouseDown
        建照_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 執照_dtp_MouseDown(sender As Object, e As EventArgs) Handles 執照_dtp.MouseDown
        執照_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 太陽開始_dtp_MouseDown(sender As Object, e As EventArgs) Handles 太陽開始_dtp.MouseDown
        太陽開始_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 太陽結束_dtp_MouseDown(sender As Object, e As EventArgs) Handles 太陽結束_dtp.MouseDown
        太陽結束_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 免租開始1_dtp_MouseDown(sender As Object, e As EventArgs) Handles 免租開始1_dtp.MouseDown
        免租開始1_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 免租結束1_dtp_MouseDown(sender As Object, e As EventArgs) Handles 免租結束1_dtp.MouseDown
        免租結束1_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 免租開始_dtp_MouseDown(sender As Object, e As EventArgs) Handles 免租開始_dtp.MouseDown
        免租開始_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 免租結束_dtp_MouseDown(sender As Object, e As EventArgs) Handles 免租結束_dtp.MouseDown
        免租結束_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 租期開始1_dtp_MouseDown(sender As Object, e As EventArgs) Handles 租期開始1_dtp.MouseDown
        租期開始1_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 租期結束1_dtp_MouseDown(sender As Object, e As EventArgs) Handles 租期結束1_dtp.MouseDown
        租期結束1_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 租期開始_dtp_MouseDown(sender As Object, e As EventArgs) Handles 租期開始_dtp.MouseDown
        租期開始_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 租期結束_dtp_MouseDown(sender As Object, e As EventArgs) Handles 租期結束_dtp.MouseDown
        租期結束_dtp.CustomFormat = "yyyy/MM/dd"
    End Sub
    Private Sub 租期開始_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 租期開始_dtp.ValueChanged
        If 租期開始_dtp.Text = " " Or 租期結束_dtp.Text = " " Then : 土地租期_nup.Value = 0 : Else : 土地租期_nup.Value = Year(租期結束_dtp.Value) - Year(租期開始_dtp.Value) : End If
    End Sub
    Private Sub 租期結束_dtp_ValueChanged(sender As Object, e As EventArgs) Handles 租期結束_dtp.ValueChanged
        If 租期開始_dtp.Text = " " Or 租期結束_dtp.Text = " " Then : 土地租期_nup.Value = 0 : Else : 土地租期_nup.Value = Year(租期結束_dtp.Value) - Year(租期開始_dtp.Value) : End If
    End Sub
    Private Sub 土地租期_nup_ValueChanged(sender As Object, e As EventArgs) Handles 土地租期_nup.ValueChanged
        租期結束_dtp.Value = 租期開始_dtp.Value.AddYears(土地租期_nup.Value)
    End Sub
    Private Sub 免租開始_dtp_ValueChanged_1(sender As Object, e As EventArgs) Handles 免租開始_dtp.ValueChanged
        Dim startDate As DateTime = 免租開始_dtp.Value : Dim endDate As DateTime = 免租結束_dtp.Value
        Dim monthDifference As Integer = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month : 免租_nud.Value = monthDifference
    End Sub
    Private Sub 免租結束_dtp_ValueChanged_1(sender As Object, e As EventArgs) Handles 免租結束_dtp.ValueChanged
        Dim startDate As DateTime = 免租開始_dtp.Value : Dim endDate As DateTime = 免租結束_dtp.Value
        Dim monthDifference As Integer = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month : 免租_nud.Value = monthDifference
    End Sub
    Private Sub 免租_nud_ValueChanged(sender As Object, e As EventArgs) Handles 免租_nud.ValueChanged
        Dim startDate As DateTime = 免租開始_dtp.Value : Dim monthDifference As Integer = CInt(免租_nud.Value)
        Dim endDate As DateTime = startDate.AddMonths(monthDifference) : 免租結束_dtp.Value = endDate
    End Sub
    Private Sub 客戶資料表_bt_Click(sender As Object, e As EventArgs) Handles 客戶資料表_bt.Click
        If 流水號_tb.Text = "" Then : MsgBox("請先選擇案件資料，才能繼續後續的操作!!") : Else
            If 視窗2_pl.Visible = False Then
                視窗2_pl.Location = New System.Drawing.Point((Panel1.Size.Width - 視窗2_pl.Size.Width) / 2, (Panel1.Size.Height - 視窗2_pl.Size.Height) / 2)
                視窗2_pl.Visible = True : 視窗2_pl.BringToFront() : Set_客戶清單()
            Else : End If
        End If
    End Sub
    Private Sub 關鍵字1_tb_KeyDown(sender As Object, e As KeyEventArgs) Handles 關鍵字1_tb.KeyDown
        If e.KeyCode = Keys.Enter Then : Set_客戶清單() : End If
    End Sub
    Private Sub 分類2_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 分類2_cb.SelectedIndexChanged
        Set_客戶清單()
    End Sub
    Private Sub 查詢_bt_Click(sender As Object, e As EventArgs) Handles 查詢_bt.Click
        Set_客戶清單()
    End Sub
    Private Sub 附件1_pb_Click(sender As Object, e As EventArgs) Handles 附件1_pb.Click
        If 附件1_pb.Image IsNot Nothing Then : 圖片傳遞 = 附件1_pb.Image : 圖片放大視窗.ShowDialog() : End If
    End Sub
    Private Sub 附件_pb_Click(sender As Object, e As EventArgs) Handles 附件_pb.Click
        If 附件_pb.Image IsNot Nothing Then : 圖片傳遞 = 附件_pb.Image : 圖片放大視窗.ShowDialog() : End If
    End Sub
    Private Sub 列印_bt_Click(sender As Object, e As EventArgs) Handles 列印_bt.Click
        MsgBox("功能開發中")
    End Sub
    Private Sub 列印1_bt_Click(sender As Object, e As EventArgs) Handles 列印1_bt.Click
        MsgBox("功能開發中")
    End Sub
    '----------------------自創視窗滑鼠拖曳功能---------------------------------------------------------------------------------------------------------
    Dim OldX, OldY As Long : Dim drag As Boolean
    Private Sub CanceL2_bt_Click(sender As Object, e As EventArgs) Handles CanceL2_bt.Click
        If 視窗2_pl.Visible = False Then : 視窗2_pl.Visible = True : 視窗2_pl.BringToFront() : Else
            視窗2_pl.Visible = False : End If
    End Sub
    Private Sub 視窗2_pl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseDown
        If e.Button = MouseButtons.Left Then : OldX = e.X : OldY = e.Y : drag = True : End If
    End Sub
    Private Sub 視窗2_pl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseMove
        If drag Then : 視窗2_pl.Left = 視窗2_pl.Left + e.X - OldX : 視窗2_pl.Top = 視窗2_pl.Top + e.Y - OldY : End If
    End Sub
    Private Sub 視窗2_pl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 視窗2_pl.MouseUp
        drag = False
    End Sub
End Class