Imports System.Text.RegularExpressions Imports CefSharp Imports CefSharp.WinForms Public Class 測試介面 Dim 不寫入 As Boolean Dim ZX1, ZY1, ZX2(5), ZY2(5), SX1(5), SY1(5) As Double Private Sub 測試介面_Load(sender As Object, e As EventArgs) Handles MyBase.Load ZX1 = Panel1.Size.Width : ZY1 = Panel1.Size.Height ZX2(0) = Button1.Size.Width : ZY2(0) = Button1.Size.Height : SX1(0) = Button1.Location.X : SY1(0) = Button1.Location.Y ZX2(1) = Button2.Size.Width : ZY2(1) = Button2.Size.Height : SX1(1) = Button2.Location.X : SY1(1) = Button2.Location.Y ZX2(2) = Button3.Size.Width : ZY2(2) = Button3.Size.Height : SX1(2) = Button3.Location.X : SY1(2) = Button3.Location.Y ZX2(3) = Button4.Size.Width : ZY2(3) = Button4.Size.Height : SX1(3) = Button4.Location.X : SY1(3) = Button4.Location.Y ZX2(4) = Button5.Size.Width : ZY2(4) = Button5.Size.Height : SX1(4) = Button5.Location.X : SY1(4) = Button5.Location.Y ZX2(5) = Button6.Size.Width : ZY2(5) = Button6.Size.Height : SX1(5) = Button6.Location.X : SY1(5) = Button6.Location.Y 準備扣_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing 準備扣_dgv.ColumnHeadersHeight = 40 : 準備扣_dgv.AllowUserToAddRows = False 準備扣_dgv.Columns(1).DefaultCellStyle.Format = "#,##0.0" 準備扣_dgv.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight 準備扣_dgv.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight 準備扣_dgv.Columns(0).ReadOnly = True : 準備扣_dgv.Columns(1).ReadOnly = True : 準備扣_dgv.Columns(2).ReadOnly = True 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "加總" : 準備扣_dgv.Rows(0).Cells(1).Value = "0" : 準備扣_dgv.Rows(0).Cells(2).Value = "A[8]+A[7]+A[6]+A[5]+A[4]+A[3]+A[2]+A[1]" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "水管" : 準備扣_dgv.Rows(0).Cells(1).Value = "12" : 準備扣_dgv.Rows(0).Cells(2).Value = "(A[5]+A[6]+A[7])/100*3*17" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "水泥" : 準備扣_dgv.Rows(0).Cells(1).Value = "13" : 準備扣_dgv.Rows(0).Cells(2).Value = "(A[5]+A[6])/100*70+1500000" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "沙子" : 準備扣_dgv.Rows(0).Cells(1).Value = "14" : 準備扣_dgv.Rows(0).Cells(2).Value = "A[5]/100*50+521000" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "磚頭" : 準備扣_dgv.Rows(0).Cells(1).Value = "123" : 準備扣_dgv.Rows(0).Cells(2).Value = "1500*1200" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "螺絲" : 準備扣_dgv.Rows(0).Cells(1).Value = "234" : 準備扣_dgv.Rows(0).Cells(2).Value = "500*2000" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "焊條" : 準備扣_dgv.Rows(0).Cells(1).Value = "235" : 準備扣_dgv.Rows(0).Cells(2).Value = "3000*200" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "人工" : 準備扣_dgv.Rows(0).Cells(1).Value = "564" : 準備扣_dgv.Rows(0).Cells(2).Value = "150000*25" 準備扣_dgv.Rows.Insert(0) : 準備扣_dgv.Rows(0).Cells(0).Value = "運費" : 準備扣_dgv.Rows(0).Cells(1).Value = "897" : 準備扣_dgv.Rows(0).Cells(2).Value = "5000000" End Sub Private Sub 測試介面_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged If ZX1 = 0 Or ZY1 = 0 Or ZX2(0) = 0 Or ZY2(0) = 0 Then : Else Dim WX As Double = Panel1.Size.Width : Dim WY As Double = Panel1.Size.Height Button1.Size = New Size(WX * ZX2(0) / ZX1, WY * ZY2(0) / ZY1) : Button1.Location = New Point(WX * SX1(0) / ZX1, WY * SY1(0) / ZY1) Button2.Size = New Size(WX * ZX2(1) / ZX1, WY * ZY2(1) / ZY1) : Button2.Location = New Point(WX * SX1(1) / ZX1, WY * SY1(1) / ZY1) Button3.Size = New Size(WX * ZX2(2) / ZX1, WY * ZY2(2) / ZY1) : Button3.Location = New Point(WX * SX1(2) / ZX1, WY * SY1(2) / ZY1) Button4.Size = New Size(WX * ZX2(3) / ZX1, WY * ZY2(3) / ZY1) : Button4.Location = New Point(WX * SX1(3) / ZX1, WY * SY1(3) / ZY1) Button5.Size = New Size(WX * ZX2(4) / ZX1, WY * ZY2(4) / ZY1) : Button5.Location = New Point(WX * SX1(4) / ZX1, WY * SY1(4) / ZY1) Button6.Size = New Size(WX * ZX2(5) / ZX1, WY * ZY2(5) / ZY1) : Button6.Location = New Point(WX * SX1(5) / ZX1, WY * SY1(5) / ZY1) End If End Sub Private Sub 準備扣_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 準備扣_dgv.CellClick If e.RowIndex = -1 Then : Else If 鎖定_ch.Checked = True Then If 目前_tb.Text <> e.RowIndex Then 算式_tb.Text = 算式_tb.Text & "A[" & e.RowIndex + 1 & "]" End If Else If 編輯_ch.Checked = True Then 計算_bt.PerformClick() : 目前_tb.Text = e.RowIndex : 編輯_ch.Checked = True : 算式_tb.Text = 準備扣_dgv.Rows(e.RowIndex).Cells(2).Value.ToString Else If 目前_tb.Text = "" Then 目前_tb.Text = e.RowIndex : 編輯_ch.Checked = True : 算式_tb.Text = 準備扣_dgv.Rows(e.RowIndex).Cells(2).Value.ToString Else If 目前_tb.Text <> e.RowIndex Then 算式_tb.Text = 算式_tb.Text & "A[" & e.RowIndex + 1 & "]" 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 For G As Integer = 0 To 準備扣_dgv.Rows.Count - 1 不寫入 = True : 算式_tb.Text = 準備扣_dgv.Rows(G).Cells(2).Value.ToString : 不寫入 = False If 算式_tb.Text <> "" Then Try Dim A As String = 算式_tb.Text ' 或是 "A[0]+A[1]-A[2]+A[3]-A[4]+A[5]-A[6]+A[7]" Dim PP(99) As Integer : Dim pattern As String = "A\[(\d+)\]" : Dim regex As New Regex(pattern) Dim matches As MatchCollection = regex.Matches(A) : Dim SD As Integer = 0 For Each match As Match In matches Dim value As Integer = Integer.Parse(match.Groups(1).Value) : PP(SD) = value : If PP(SD) = 0 Then : Exit For : End If : SD += 1 Next Dim replacedA As String = A For i As Integer = 0 To PP.Length - 1 If PP(i) = 0 Then : Exit For : End If : replacedA = replacedA.Replace("A[" & PP(i) & "]", CDbl(準備扣_dgv.Rows(PP(i) - 1).Cells(1).Value.ToString)) Next : Console.WriteLine(replacedA) : 不寫入 = True : 算式_tb.Text = replacedA : 不寫入 = False Dim dt As New DataTable() dt.Columns.Add("Result", GetType(Double)) dt.Rows.Add(dt.Compute(算式_tb.Text, "")) 結果_tb.Text = Convert.ToDouble(dt.Rows(0)("Result")) : 準備扣_dgv.Rows(G).Cells(1).Value = Strings.Format(CDbl(結果_tb.Text), "#,##0.00") Catch ex As Exception MsgBox(ex.Message) End Try End If Next If 目前_tb.Text <> "" Then 不寫入 = True : Dim XC As Integer = 目前_tb.Text : 算式_tb.Text = 準備扣_dgv.Rows(XC).Cells(2).Value.ToString : 不寫入 = False Else : 算式_tb.Text = "" : End If : 目前_tb.Text = "" : 編輯_ch.Checked = False : 鎖定_ch.Checked = False 結果_tb.Text = Strings.Format(CDbl(結果_tb.Text), "#,##0.00") End Sub Private Sub 算式_tb_Click(sender As Object, e As EventArgs) Handles 算式_tb.Click If 目前_tb.Text <> "" Then : 鎖定_ch.Checked = True : End If End Sub Private Sub 算式_tb_TextChanged(sender As Object, e As EventArgs) Handles 算式_tb.TextChanged If 不寫入 = False Then : If 目前_tb.Text = "" Then : Else : Dim XC As Integer = 目前_tb.Text : 準備扣_dgv.Rows(XC).Cells(2).Value = 算式_tb.Text : End If : End If End Sub End Class