thongkorn โพสต์ 2019-3-18 13:24:39

[VB.NET] แจกฟรีโค้ดงานต้นแบบจำลองการขายเบอเกอร์และเครื่องดื่ม

http://www.g2gnet.com/webboard/images/vbnet/burgerorderv2.png

ที่ต้องตั้งชื่อว่า งานต้นแบบจำลองการขาย ก็เพราะว่าแอดมินนำเสนอวิธีการออกแบบโปรแกรมด้วยการจำลองข้อมูลตัวอย่างขึ้นมาก่อน โดยที่ยังไม่ได้ออกแบบตารางข้อมูลแต่อย่างใด หากท่านที่พึ่งเข้ามาอ่านต้องย้อนกลับไปดูในเรื่อง แจกฟรีโค้ดต้นฉบับ โปรแกรมต้นแบบร้านทองก้อนเบอเกอร์แดนซ์ ซึ่งจะทำให้เรามองออกว่าควรจะออกแบบฟิลด์ข้อมูลรายการอาหาร/เครื่องดื่มอะไรบ้าง พอมาถึงโค้ดชุดนี้แอดมินจะดึงข้อมูลจากตารางอาหาร/เครื่องดื่ม เพื่อมาทำการสร้างปุ่มรายการในแบบไดนามิคตามจำนวนรายการ ให้ปรากฏอยู่บนหน้าจอภาพ จากนั้นก็คลิ๊กทำการขายออกไป โดยรายการอาหาร/เครื่องดื่มจะไปปรากฏอยู่ในตารางกริด หากเป็นการเลือกรายการเดิม จะทำการเพิ่มจำนวนเข้าไป แต่หากเป็นรายการที่ยังไม่ได้ถูกเลือก ก็จะเพิ่มรายการใหม่เข้าไปยังแถวรายการของตารางกริด สุดท้ายคือการคิดเงิน ซึ่งแอดมินได้ทำการแจกโค้ดเอาไว้ล่วงหน้ามาก่อนแล้ว ฟอร์มการชำระเงินด้วยการสร้างปุ่มคำสั่ง (Button) แบบไดนามิค...

โค้ดในชุดนี้ แอดมินมีเจตนาที่จะสื่อความหมายเอาไว้ให้เห็นว่า เรานำเอาผลลัพธ์ที่ได้มาทำการออกแบบและจัดเก็บข้อมูลตารางการขายอาหาร/เครื่องดื่มได้อย่างไรบ้าง ว่าง่ายๆคือนำเอาข้อมูลเอ้าพุทย้อนกลับมาคิดเป็นอินพุทนั่นเอง ...

มาดูโค้ดในส่วนที่สำคัญ ...
การ Query จำนวนปุ่มคำสั่งมาจากจำนวนของรายการอาหาร/เครื่องดื่มในแบบไดนามิค (ทำตัวอย่างเอาไว้ให้ 2 กลุ่ม)
      ' / ---------------------------------------------------------------
      ' / การใช้งานจริง จะต้องค้นหาจำนวนของกลุ่มสินค้าเข้ามาก่อน
      ' / เพื่อกำหนดจำนวน TabControl ได้
      ' / ---------------------------------------------------------------
      ' / ใส่ Panel ลงบนฟอร์ม
      ' / Create a tabpage
      Dim tabPageRef As New TabPage
      ' / Set the tabpage to be your desired tab
      tabPageRef = TabControl1.TabPages(0)
      '// Create Panel
      With pn1
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(TabControl1.Width - 10, TabControl1.Height - 30)
            .BackColor = Color.Moccasin
            .AutoScroll = True
            .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
      End With
      'Add the panel
      tabPageRef.Controls.Add(pn1)
      strSQL = _
            " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
            " FROM(Food) " & _
            " WHERE Food.CategoryFK = 2 " & _
            " ORDER BY FoodPK "
      '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, รายการ, Panel)
      Call AddButtonsToForm(3, strSQL, pn1)

      '// New Panel
      'set the tabpage to be your desired tab
      tabPageRef = TabControl1.TabPages(1)
      '// Create Panel
      With pn2
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(TabControl1.Width - 10, TabControl1.Height - 30)
            .BackColor = Color.Moccasin
            .AutoScroll = True
            .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
      End With
      'add the panel
      tabPageRef.Controls.Add(pn2)
      strSQL = _
            " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
            " FROM(Food) " & _
            " WHERE Food.CategoryFK = 3 " & _
            " ORDER BY FoodPK "
      '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, รายการ, Panel)
      Call AddButtonsToForm(3, strSQL, pn2)
การสร้างปุ่มคำสั่งลงใน TabControl และ Panel ...    
    ' / ---------------------------------------------------------------
    '// เพิ่มปุ่มคำสั่ง (Button Control) แบบ Run Time
    Private Sub AddButtonsToForm(ByVal ColCount As Byte, ByVal sql As String, pn As Panel)
      Dim Buttons As New Dictionary(Of String, Button)
      Dim img As String
      Dim Rec As Integer = 0
      Try
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            Cmd = New OleDbCommand(sql, Conn)
            DR = Cmd.ExecuteReader
            ' / Make sure Primary Key only one and not duplicate
            While DR.Read()
                If DR.HasRows Then
                  Dim B As New Button
                  pn.Controls.Add(B)
                  With B
                        .Height = 140
                        .Width = 140
                        .Left = (Rec Mod ColCount) * B.Width
                        .Top = (Rec \ ColCount) * B.Height
                        .Text = DR.Item("FoodName") & vbCrLf & Format(CDbl(DR.Item("PriceCash")), "#,##0.00") & "B."
                        Buttons.Add(B.Text, B)
                        '// นำค่า Primary Key ไปเก็บไว้ที่คุณสมบัติ Tag เมื่อกดปุ่มคำสั่งจะใช้ค่านี้ไปค้นหาจากฐานข้อมูล
                        .Tag = DR.Item("FoodPK")
                        '// อ่านค่ารูปภาพ และตรวจสอบการมีอยู่จริงของภาพด้วยฟังค์ชั่น GetImages
                        img = GetImages(DR.Item("PictureFood"))
                        '// ใส่ภาพลงไปในปุ่มคำสั่ง Button
                        .BackgroundImage = New System.Drawing.Bitmap(img)
                        '//
                        .Cursor = Cursors.Hand
                        .BackgroundImageLayout = ImageLayout.Stretch
                        .Font = New Font("Century Gothic", 14, FontStyle.Bold)
                        .ForeColor = Color.LightYellow
                        .TextImageRelation = TextImageRelation.ImageAboveText
                        .TextAlign = ContentAlignment.BottomCenter
                        .UseVisualStyleBackColor = True
                  End With
                  Rec += 1
                  '// Force events handler.
                  AddHandler B.Click, AddressOf ClickButton
                End If
            End While
            DR.Close()
      Catch ex As Exception
            MessageBox.Show(ex.Message)
      End Try

    End Sub
โค้ดในการคลิ๊กที่ปุ่มคำสั่งเลือกรายการอาหาร/เครื่องดื่ม หากมีรายการเดิมจะทำการเพิ่มจำนวน หากไม่มีต้องค้นรายการมาเพิ่มลงในตารางกริด
    ' / ---------------------------------------------------------------
    ' / เพิ่มรายการแถวใหม่ (UnBound Data)
    Private Sub AddRow(ByVal PK As Integer) ', ByVal Description As String, ByVal UnitPrice As Double)
      '// ใช้เพื่อค้นหาข้อมูลในตารางกริดก่อน
      Dim blnExist As Boolean = False
      '// ตรวจสอบว่า Primary Key มีอยู่ในตารางกริดหรือไม่ (หากมีให้ +เพิ่ม, หากไม่มีค่อยไปค้นหาข้อมูล)
      For Rec As Integer = 0 To dgvData.RowCount - 1
            ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
            If dgvData.Rows(Rec).Cells(0).Value = PK Then
                ' / ให้บวกจำนวนที่เลือกเพิ่มขึ้นอีก 1 (Quantity = Quantity + 1)
                dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value + 1
                lblLastPrice.Text = "Last Price: " & Format(CDbl(dgvData.Rows(Rec).Cells(2).Value), "#,##0.00")
                '// เจอข้อมูลเดิม
                blnExist = True
                '// ออกจากลูปไปเลย เพื่อไม่ให้เสียเวลา
                Exit For
            End If
      Next
      '// ไม่พบข้อมูลในตารางกริด ก็ทำการเรียกจากตารางข้อมูลเข้ามาแสดงผล
      If Not blnExist Then
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            strSQL = _
                " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
                " FROM(Food) " & _
                " WHERE Food.FoodPK = " & PK & _
                " ORDER BY FoodPK "
            Cmd = New OleDbCommand
            Cmd.Connection = Conn
            Cmd.CommandText = strSQL
            DR = Cmd.ExecuteReader
            DR.Read()
            '// เพิ่มรายการแถวสินค้าเข้าไปใหม่ Primary Key (ถูกซ่อน), ชื่อสินค้า, ราคาขาย, จำนวน = 1
            Dim row As String() = New String() {PK, DR.Item("FoodName").ToString, Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00"), "1"}
            dgvData.Rows.Add(row)
      End If
      '// หาจำนวนเงินรวมใหม่
      Call CalAmount()
      '// เป็นสินค้าตัวใหม่ที่ถูกเพิ่มเข้ามาให้อยู่ในแถว แสดงราคาสินค้าล่าสุด
      If Not blnExist Then
            lblLastPrice.Text = "Last Price: " & Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00")
      End If
      DR.Close()
      '// โฟกัสไปที่ DataGridView แล้วย้ายไปแถวล่าสุด
      dgvData.Focus()
      SendKeys.Send("^{END}")
    End Sub
มาดูโค้ดฉบับเต็มกันเถอะ ... (ฟอร์มหลัก frmMainOrder.vb)
' / ---------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / More Info: http://www.g2gnet.com/webboard
' /
' / Purpose: Prototype of Burger Order System V2.
' / Microsoft Visual Basic .NET (2010) + MS Access 2007+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / ---------------------------------------------------------------
Imports System.Data.OleDb

Public Class frmMainOrder

    Private pn1 As New Panel()
    Private pn2 As New Panel
    '// รวมจำนวนเงิน
    Dim Amount As Double

    Private Sub frmMainOrder_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Select Case e.KeyCode
            Case Keys.F8
                btnPayment_Click(sender, e)
            Case Keys.F10
                Me.Close()
      End Select
    End Sub

    ' / ---------------------------------------------------------------
    Private Sub frmMainOrder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      '// Cennect MS Access DB.
      Conn = MyDBModule()
      '//
      Amount = "0.00"
      Call InitGrid()

      ' / ---------------------------------------------------------------
      ' / การใช้งานจริง จะต้องค้นหาจำนวนของกลุ่มสินค้าเข้ามาก่อน
      ' / เพื่อกำหนดจำนวน TabControl ได้
      ' / ---------------------------------------------------------------
      ' / ใส่ Panel ลงบนฟอร์ม
      ' / Create a tabpage
      Dim tabPageRef As New TabPage
      ' / Set the tabpage to be your desired tab
      tabPageRef = TabControl1.TabPages(0)
      '// Create Panel
      With pn1
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(TabControl1.Width - 10, TabControl1.Height - 30)
            .BackColor = Color.Moccasin
            .AutoScroll = True
            .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
      End With
      'Add the panel
      tabPageRef.Controls.Add(pn1)
      strSQL = _
            " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
            " FROM(Food) " & _
            " WHERE Food.CategoryFK = 2 " & _
            " ORDER BY FoodPK "
      '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, รายการ, Panel)
      Call AddButtonsToForm(3, strSQL, pn1)

      '// New Panel
      'set the tabpage to be your desired tab
      tabPageRef = TabControl1.TabPages(1)
      '// Create Panel
      With pn2
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(TabControl1.Width - 10, TabControl1.Height - 30)
            .BackColor = Color.Moccasin
            .AutoScroll = True
            .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
      End With
      'add the panel
      tabPageRef.Controls.Add(pn2)
      strSQL = _
            " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
            " FROM(Food) " & _
            " WHERE Food.CategoryFK = 3 " & _
            " ORDER BY FoodPK "
      '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, รายการ, Panel)
      Call AddButtonsToForm(3, strSQL, pn2)

    End Sub

    ' / ---------------------------------------------------------------
    '// เพิ่มปุ่มคำสั่ง (Button Control) แบบ Run Time
    Private Sub AddButtonsToForm(ByVal ColCount As Byte, ByVal sql As String, pn As Panel)
      Dim Buttons As New Dictionary(Of String, Button)
      Dim img As String
      Dim Rec As Integer = 0
      Try
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            Cmd = New OleDbCommand(sql, Conn)
            DR = Cmd.ExecuteReader
            ' / Make sure Primary Key only one and not duplicate
            While DR.Read()
                If DR.HasRows Then
                  Dim B As New Button
                  pn.Controls.Add(B)
                  With B
                        .Height = 140
                        .Width = 140
                        .Left = (Rec Mod ColCount) * B.Width
                        .Top = (Rec \ ColCount) * B.Height
                        .Text = DR.Item("FoodName") & vbCrLf & Format(CDbl(DR.Item("PriceCash")), "#,##0.00") & "B."
                        Buttons.Add(B.Text, B)
                        '// นำค่า Primary Key ไปเก็บไว้ที่คุณสมบัติ Tag เมื่อกดปุ่มคำสั่งจะใช้ค่านี้ไปค้นหาจากฐานข้อมูล
                        .Tag = DR.Item("FoodPK")
                        '// อ่านค่ารูปภาพ และตรวจสอบการมีอยู่จริงของภาพด้วยฟังค์ชั่น GetImages
                        img = GetImages(DR.Item("PictureFood"))
                        '// ใส่ภาพลงไปในปุ่มคำสั่ง Button
                        .BackgroundImage = New System.Drawing.Bitmap(img)
                        '//
                        .Cursor = Cursors.Hand
                        .BackgroundImageLayout = ImageLayout.Stretch
                        .Font = New Font("Century Gothic", 14, FontStyle.Bold)
                        .ForeColor = Color.LightYellow
                        .TextImageRelation = TextImageRelation.ImageAboveText
                        .TextAlign = ContentAlignment.BottomCenter
                        .UseVisualStyleBackColor = True
                  End With
                  Rec += 1
                  '// Force events handler.
                  AddHandler B.Click, AddressOf ClickButton
                End If
            End While
            DR.Close()
      Catch ex As Exception
            MessageBox.Show(ex.Message)
      End Try

    End Sub

    Private Function GetImages(ByVal picData As String) As String
      '// Show picture in cell.
      If picData <> "" Then
            '// First, before load data into DataGrid and check File exists or not?
            If Dir(strPathImages & picData) = "" Then
                GetImages = strPathImages & "FoodStuff.png"
            Else
                GetImages = strPathImages & picData
            End If

            '// ไม่มีข้อมูลภาพ
      Else
            GetImages = strPathImages & "FoodStuff.png"
      End If
    End Function

    '// Click Button event, get the text of button
    Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim btn As Button = sender
      'MessageBox.Show("คุณคลิ๊ก [" + btn.Text + "]" & vbCrLf & "Tag=" & btn.Tag & " จะเป็นค่า Primary Key")
      Call AddRow(btn.Tag)
    End Sub

    ' / ---------------------------------------------------------------
    ' / เพิ่มรายการแถวใหม่ (UnBound Data)
    Private Sub AddRow(ByVal PK As Integer) ', ByVal Description As String, ByVal UnitPrice As Double)
      '// ใช้เพื่อค้นหาข้อมูลในตารางกริดก่อน
      Dim blnExist As Boolean = False
      '// ตรวจสอบว่า Primary Key มีอยู่ในตารางกริดหรือไม่ (หากมีให้ +เพิ่ม, หากไม่มีค่อยไปค้นหาข้อมูล)
      For Rec As Integer = 0 To dgvData.RowCount - 1
            ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
            If dgvData.Rows(Rec).Cells(0).Value = PK Then
                ' / ให้บวกจำนวนที่เลือกเพิ่มขึ้นอีก 1 (Quantity = Quantity + 1)
                dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value + 1
                lblLastPrice.Text = "Last Price: " & Format(CDbl(dgvData.Rows(Rec).Cells(2).Value), "#,##0.00")
                '// เจอข้อมูลเดิม
                blnExist = True
                '// ออกจากลูปไปเลย เพื่อไม่ให้เสียเวลา
                Exit For
            End If
      Next
      '// ไม่พบข้อมูลในตารางกริด ก็ทำการเรียกจากตารางข้อมูลเข้ามาแสดงผล
      If Not blnExist Then
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            strSQL = _
                " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
                " FROM(Food) " & _
                " WHERE Food.FoodPK = " & PK & _
                " ORDER BY FoodPK "
            Cmd = New OleDbCommand
            Cmd.Connection = Conn
            Cmd.CommandText = strSQL
            DR = Cmd.ExecuteReader
            DR.Read()
            '// เพิ่มรายการแถวสินค้าเข้าไปใหม่ Primary Key (ถูกซ่อน), ชื่อสินค้า, ราคาขาย, จำนวน = 1
            Dim row As String() = New String() {PK, DR.Item("FoodName").ToString, Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00"), "1"}
            dgvData.Rows.Add(row)
      End If
      '// หาจำนวนเงินรวมใหม่
      Call CalAmount()
      '// เป็นสินค้าตัวใหม่ที่ถูกเพิ่มเข้ามาให้อยู่ในแถว แสดงราคาสินค้าล่าสุด
      If Not blnExist Then
            lblLastPrice.Text = "Last Price: " & Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00")
      End If
      DR.Close()
      '// โฟกัสไปที่ DataGridView แล้วย้ายไปแถวล่าสุด
      dgvData.Focus()
      SendKeys.Send("^{END}")
    End Sub

    Private Sub CalAmount()
      '// หาจำนวนเงินรวมใหม่
      Amount = 0
      For i = 0 To dgvData.RowCount - 1
            Amount = Amount + (CDbl(dgvData.Rows(i).Cells(2).Value) * CInt(dgvData.Rows(i).Cells(3).Value))
      Next
      '// แสดงผลราคารวม
      lblAmount.Text = "Total: " & Format(Amount, "#,##0.00")
    End Sub

    ' / ---------------------------------------------------------------
    '// Initialize DataGridView @Run Time
    Private Sub InitGrid()
      '// ตั้งค่าจำนวนหลัก (Columns)
      With dgvData
            .Columns.Add("FoodPK", "FoodPK")
            .Columns.Add("FoodName", "รายการ")
            .Columns.Add("PriceCash", "ราคา")
            .Columns.Add("Quantity", "จำนวน")
            '//
            .Columns(0).Visible = False
            '.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
            '// UnitPrice
            With .Columns(2)
                .ValueType = GetType(Decimal)
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            '// Quantity
            With .Columns(3)
                .ValueType = GetType(Double)
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
      End With
      '//
      With dgvData
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ReadOnly = True
            '//
            .Font = New Font("Century Gothic", 14)
            .RowTemplate.Height = 40
            '.RowTemplate.MinimumHeight = 32
            ' Autosize Column
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            '// Even-Odd Color
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            ' Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.Navy
                .ForeColor = Color.Black
                .Font = New Font("Century Gothic", 16, FontStyle.Bold)
                .WrapMode = DataGridViewTriState.False
            End With
      End With

    End Sub

    ' / ---------------------------------------------------------------
    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
      If dgvData.RowCount <= 0 Then Exit Sub
      dgvData.Focus()
      For Rec As Integer = 0 To dgvData.RowCount - 1
            '// ใช้หลัก PK เป็นกุญแจหลักในการอ้างอิง
            Dim PK As Integer = dgvData.CurrentRow.Cells(0).Value
            ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
            If dgvData.Rows(Rec).Cells(0).Value = PK Then
                '// หากพบข้อมูลเดิม แต่มีจำนวน = 1 ก็ลบออกไปได้เลย
                If dgvData.Rows(Rec).Cells(3).Value = 1 Then
                  dgvData.Rows.Remove(dgvData.CurrentRow)
                  dgvData.Refresh()
                  '// ไม่ต้องไปลบอีกรอบ
                  Exit For
                Else
                  ' / ให้ลบจำนวนลง 1 (Quantity = Quantity - 1)
                  dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value - 1
                  Exit For
                End If
            End If
      Next
      '// รวมจำนวนเงิน
      Call CalAmount()
    End Sub

    ' / ---------------------------------------------------------------
    ' / คิดเงิน
    Private Sub btnPayment_Click(sender As System.Object, e As System.EventArgs) Handles btnPayment.Click
      If dgvData.RowCount <= 0 Then Exit Sub
      frmPayment.ShowDialog()
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
      dgvData.Rows.Clear()
      Amount = "0.00"
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    Private Sub frmMainOrder_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      If Conn.State = ConnectionState.Open Then Conn.Close()
      Application.Exit()
    End Sub

End Class
ฟอร์มการคิดเงิน (frmPayment.vb)
' / ---------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / More Info: http://www.g2gnet.com/webboard
' /
' / Purpose: Payment with Button Array.
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / ---------------------------------------------------------------
Public Class frmPayment

    Private Sub frmPayment_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Select Case e.KeyCode
            Case Keys.F8
                btnCash_Click(sender, e)
            Case Keys.F10
                Me.Close()
      End Select
    End Sub

    Private Sub frmPayment_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      '// สร้างปุ่ม 0 - 9
      Call AddButtons(3, 9)
      '//
      txtAmount.Text = Format(CDbl(Mid(frmMainOrder.lblAmount.Text, 7, Len(frmMainOrder.lblAmount.Text) - 6)), "#,##")
      txtCash.Text = "0"
      txtChange.Text = "0.00"
      '//
      txtCash.Focus()
    End Sub

    '// เพิ่มปุ่มคำสั่ง (Button Control)
    Private Sub AddButtons(ByVal ColCount As Byte, ByVal MaxBtn As Byte)
      Dim Buttons As New Dictionary(Of String, Button)
      For i As Integer = 0 To MaxBtn - 1
            Dim B As New Button
            Me.Panel1.Controls.Add(B)
            Me.Panel1.Width = ColCount * 100
            With B
                .Height = 60
                .Width = 66
                .Left = (i Mod ColCount) * B.Width
                .Top = (i \ ColCount) * B.Height
                .Text = i + 1
                Buttons.Add(B.Text, B)
                .Tag = i + 1
                .Cursor = Cursors.Hand
                .Font = New Font("Tahoma", 14, FontStyle.Bold)
            End With
            '// Force events handler.
            AddHandler B.Click, AddressOf ClickButton
      Next
    End Sub

    '// Click Button event, get the text of button
    Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim btn As Button = sender
      'MessageBox.Show("คุณคลิ๊ก [" + btn.Text + "]" & vbCrLf & "Tag=" & btn.Tag)
      txtCash.Text = CDbl(txtCash.Text + btn.Text)
      Call CalSum()
    End Sub

    ' / ---------------------------------------------------------------
    Private Sub btnCash_Click(sender As System.Object, e As System.EventArgs) Handles btnCash.Click
      If txtCash.Text = "" Or Len(txtCash.Text) = 0 Or Val(txtCash.Text) = 0 Then
            txtCash.Text = 0
            txtChange.Text = "0.00"
            txtCash.Focus()
            Exit Sub
      End If
      '// ตรวจสอบยอดเงิน
      If CDbl(txtCash.Text) - CDbl(txtAmount.Text) < 0 Then
            MessageBox.Show("จำนวนเงินยังไม่ครบ กรุณาแก้ไขข้อมูลใหม่ด้วย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtCash.Focus()
      Else
            MessageBox.Show("ทำรายการเสร็จสิ้นเรียบร้อย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
            '// สั่งเคลียร์รายการข้ามฟอร์มมาเลย
            With frmMainOrder
                .lblAmount.Text = "0.00"
                .lblLastPrice.Text = "0.00"
                .dgvData.Rows.Clear()
            End With
            Me.Close()
      End If
    End Sub

    Private Sub txtCash_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtCash.KeyPress
      '// รับค่าเฉพาะตัวเลขเท่านั้น
      e.Handled = CheckDigitOnly(Asc(e.KeyChar))
      txtCash.Text = CDbl(txtCash.Text)
    End Sub

    Private Sub txtCash_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCash.TextChanged
      Call CalSum()
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
      txtCash.Text = "0"
      txtChange.Text = "0.00"
    End Sub

    ' / ---------------------------------------------------------------
    Private Sub CalSum()
      '/ Trap Error
      If Trim(txtCash.Text) = "" Or Len(Trim(txtCash.Text)) = 0 Then
            txtCash.Text = 0
            txtChange.Text = "0.00"
            Exit Sub
      End If
      '//
      Dim dChange As Double = CDbl(txtCash.Text) - CDbl(txtAmount.Text)
      txtChange.Text = dChange
      If CDbl(dChange) < 0 Then
            txtChange.ForeColor = Color.Red
      Else
            txtChange.ForeColor = Color.Blue
      End If
      txtCash.Focus()
    End Sub

    Private Sub btn2Zero_Click(sender As System.Object, e As System.EventArgs) Handles btn2Zero.Click
      txtCash.Text = CDbl(txtCash.Text + btn2Zero.Text)
      Call CalSum()
    End Sub

    Private Sub btn1Zero_Click(sender As System.Object, e As System.EventArgs) Handles btn1Zero.Click
      txtCash.Text = CDbl(txtCash.Text + btn1Zero.Text)
      Call CalSum()
    End Sub

    Private Sub txtAmount_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
      e.Handled = True
    End Sub

    Private Sub txtChange_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtChange.KeyPress
      e.Handled = True
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub
End Class
โมดูลหากิน ... modDataBase.vb
Imports System.Data.OleDb
Module modDataBase

    Public Conn As OleDbConnection
    Public Cmd As OleDbCommand
    Public DS As DataSet
    Public DR As OleDbDataReader
    Public DA As OleDbDataAdapter
    Public DT As DataTable
    Public strSQL As String '// Major SQL
    Public strStmt As String
    Public strPathImages As String = MyPath(Application.StartupPath) & "Images\"

    Public Function MyDBModule() As OleDb.OleDbConnection
      Return New OleDb.OleDbConnection( _
            "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
            MyPath(Application.StartupPath) & "data\" & "dbFood.accdb;Persist Security Info=True")
    End Function

End Module
ฟังค์ชั่นหากิน ... modFunction
Module modFunction

    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with "\"
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
      '/ MessageBox.Show(AppPath);
      AppPath = AppPath.ToLower()
      '/ Return Value
      MyPath = AppPath.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\")
      '// If not found folder then put the \ (BackSlash) at the end.
      If Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function

    ' / --------------------------------------------------------------------------------
    ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น
    Function CheckDigitOnly(ByVal index As Integer) As Boolean
      Select Case index
            Case 48 To 57 ' เลข 0 - 9
                CheckDigitOnly = False
            Case 8, 13 ' Backspace = 8, Enter = 13
                CheckDigitOnly = False
            Case Else
                CheckDigitOnly = True
      End Select
    End Function
End Module
ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่ ...
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] แจกฟรีโค้ดงานต้นแบบจำลองการขายเบอเกอร์และเครื่องดื่ม