thongkorn โพสต์ 2023-10-12 11:47:42

[VB.NET] โค้ดในส่วนของรายละเอียดการขายสินค้า แบบไม่ต้องติดต่อกับฐานข้อมูล

http://www.g2gsoft.com/webboard/images/VBNet/posdetail.png

โค้ดในส่วนของการขายสินค้า เป็นงานต้นแบบเพื่ออธิบายให้เห็นถึงหลักการวิธีคิดของการนำข้อมูลรายการสินค้าเข้าไปเก็บไว้ในตารางกริด เพื่อทำการขายสินค้า ตามหลักการของฐานข้อมูลก็คือมี ความสัมพันธ์แบบ One To Many (ใบขายสินค้า 1 ใบ ขายสินค้าได้หลายชนิด) แต่แอดมินจะตัดส่วนในเรื่องฐานข้อมูลทิ้งออกไปก่อน เพื่อไม่ให้เกิดความสับสน โดยโค้ดชุดนี้จะแสดงให้เห็นถึงการขายแบบมาตรฐาน แบบแรกคือการที่มีสินค้า A แล้วให้รวมจำนวนสินค้า A ไว้ในรายการเดียว และอีกแบบคือสินค้าเดียวกันแต่แยกจำนวนสินค้าออกจากกัน และทั้ง 2 แบบจะสามารถแก้ไขข้อมูล เช่น ราคาหรือจำนวนสินค้าได้ในขณะที่ทำรายการ ซึ่งเราเรียกว่า In Line Edit ด้วยวิธีการนี้จะมีความเป็นมิตรกับผู้ใช้ (Friendly Use) และทั้งหมดนี้คือการทำงานที่เรียกว่า UnBound Data หรือการไม่ผูกฟิลด์ข้อมูลใดๆเอาไว้กับตารางกริด ทำให้เราสามารถควบคุมการทำงานของโปรแกรมได้ดี ปรับแต่งความยืดหยุ่นได้ตามใจชอบ ... แอดมินฝากเอาไปคิดเป็นการบ้าน เช่น กรณีที่ไม่ได้ขายแบบเงินสด จะเช็คสต็อกได้อย่างไร ...

การจำลองตารางข้อมูล (DataTable) เพื่อทำการทดสอบ โดยที่ไม่ต้องไปติดต่อกับฐานข้อมูลใดๆ ...
    ' / --------------------------------------------------------------------------------
    ' / S A M P L E ... D A T A T A B L E (Products)
    ' / --------------------------------------------------------------------------------
    Function GetDataTable() As DataTable
      '// Add Column
      Dim DT As New DataTable
      With DT.Columns
            .Add("ProductPK", GetType(Integer)) '<< Index = 0
            .Add("ProductID", GetType(String))    '<< 1
            .Add("ProductName", GetType(String)) '<< 2
            .Add("UnitPrice", GetType(Double)) '<< 3
            '// ในส่วนของการรวมจำนวนเงิน (Total) จำนวน X ราคา เราจะไม่เก็บในฐานข้อมูลให้เปลืองพื้นที่ แต่จะให้โปรแกรมคำนวณให้แทน
      End With
      '// ... Add rows for data.
      '/ ProductPK, ProductID, ProductName, UnitPrice
      '/ เหมือนการ Query เพื่อนำเอาเฉพาะข้อมูลที่ต้องการมาใช้งานเท่านั้น
      With DT.Rows
            .Add(1, "01", "กาแฟร้อน", "50.00")
            .Add(2, "02", "กาแฟเย็น", "60.00")
            .Add(3, "03", "คาปูชิโน่", "75.00")
            .Add(4, "04", "คาปูชิโน่ - ลาเต้", "80.00")
            .Add(5, "05", "เอ็กซ์เพรสโซ่", "90.00")
            .Add(6, "06", "Classic Chicken", "20.00")
            .Add(7, "07", "Mexicana", "25.00")
            .Add(8, "08", "Lemon Shrimp", "30.00")
            .Add(9, "09", "Bacon", "40.00")
            .Add(10, "10", "Spicy Shrimp", "45.00")
            .Add(11, "11", "Tex Supreme", "50.00")
            .Add(12, "12", "Fish", "55.00")
            .Add(13, "13", "Pepsi Can", "20.00")
            .Add(14, "14", "Coke Can", "20.00")
            .Add(15, "15", "7Up Can", "20.00")
            .Add(16, "16", "Pepsi 2 ลิตร", "50.00")
            .Add(17, "17", "Coke 2 ลิตร", "50.00")
            .Add(18, "18", "น้ำเปล่า", "15.00")
            .Add(19, "19", "เหล้าเป็ก", "100.00")
            .Add(20, "20", "เบียร์สิงห์ (กระป๋อง)", "50.00")
      End With
      Return DT
    End Function
มาดูโค้ดฉบับเต็มกันเถอะ ...
Public Class frmPosDetail

    Private Sub frmPosDetail_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Select Case e.KeyCode
            Case Keys.F8
                '/ Remove Row
                Call DeleteRow("btnDelRow")
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / S T A R T ... H E R E
    ' / --------------------------------------------------------------------------------
    Private Sub frmPosDetail_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Me.KeyPreview = True'/ สามารถกดปุ่มฟังค์ชั่นคีย์ลงในฟอร์มได้
      Call InitializeGrid()
      '// Set all TextBox Control to Zero and ReadOnly except txtSearch.
      For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
            If tb.Name <> "txtSearch" Then
                tb.Text = "0.00"
                tb.ReadOnly = True
            End If
      Next
      With cmbTax
            .Items.Add("None Vat")
            .Items.Add("Include Vat (7%)")
            .Items.Add("Exclude Vat (7%)")
      End With
      cmbTax.SelectedIndex = 1    '// Include VAT
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / S A M P L E ... D A T A T A B L E (Products)
    ' / --------------------------------------------------------------------------------
    Function GetDataTable() As DataTable
      '// Add Column
      Dim DT As New DataTable
      With DT.Columns
            .Add("ProductPK", GetType(Integer)) '<< Index = 0
            .Add("ProductID", GetType(String))    '<< 1
            .Add("ProductName", GetType(String)) '<< 2
            .Add("UnitPrice", GetType(Double)) '<< 3
            '// ในส่วนของการรวมจำนวนเงิน (Total) จำนวน X ราคา เราจะไม่เก็บในฐานข้อมูลให้เปลืองพื้นที่ แต่จะให้โปรแกรมคำนวณให้แทน
      End With
      '// ... Add rows for data.
      '/ ProductPK, ProductID, ProductName, UnitPrice
      '/ เหมือนการ Query เพื่อนำเอาเฉพาะข้อมูลที่ต้องการมาใช้งานเท่านั้น
      With DT.Rows
            .Add(1, "01", "กาแฟร้อน", "50.00")
            .Add(2, "02", "กาแฟเย็น", "60.00")
            .Add(3, "03", "คาปูชิโน่", "75.00")
            .Add(4, "04", "คาปูชิโน่ - ลาเต้", "80.00")
            .Add(5, "05", "เอ็กซ์เพรสโซ่", "90.00")
            .Add(6, "06", "Classic Chicken", "20.00")
            .Add(7, "07", "Mexicana", "25.00")
            .Add(8, "08", "Lemon Shrimp", "30.00")
            .Add(9, "09", "Bacon", "40.00")
            .Add(10, "10", "Spicy Shrimp", "45.00")
            .Add(11, "11", "Tex Supreme", "50.00")
            .Add(12, "12", "Fish", "55.00")
            .Add(13, "13", "Pepsi Can", "20.00")
            .Add(14, "14", "Coke Can", "20.00")
            .Add(15, "15", "7Up Can", "20.00")
            .Add(16, "16", "Pepsi 2 ลิตร", "50.00")
            .Add(17, "17", "Coke 2 ลิตร", "50.00")
            .Add(18, "18", "น้ำเปล่า", "15.00")
            .Add(19, "19", "เหล้าเป็ก", "100.00")
            .Add(20, "20", "เบียร์สิงห์ (กระป๋อง)", "50.00")
      End With
      Return DT
    End Function

    ' / --------------------------------------------------------------------------------
    ' / ตั้งค่าเริ่มต้นให้กับ DataGridView แบบ Run Time (ใช้โค้ดทั้งหมด)
    Private Sub InitializeGrid()
      With dgvData
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .ReadOnly = False
            .RowTemplate.MinimumHeight = 27
            .RowTemplate.Height = 27
            '// Declare columns type.
            Dim PK As New DataGridViewTextBoxColumn()
            Dim ProductID As New DataGridViewTextBoxColumn()
            Dim ProductName As New DataGridViewTextBoxColumn()
            Dim Quantity As New DataGridViewTextBoxColumn()
            Dim UnitPrice As New DataGridViewTextBoxColumn()
            Dim Total As New DataGridViewTextBoxColumn()
            '// Add new Columns
            dgvData.Columns.AddRange(New DataGridViewColumn() { _
                                     PK, ProductID, ProductName, Quantity, UnitPrice, Total _
                                    })
            '/ Index = 0
            With PK
                .Name = "PK"
                .HeaderText = "PK"
                .Visible = False
            End With
            '/ Index = 1
            With ProductID
                .Name = "ProductID"
                .HeaderText = "Product ID"
                .ReadOnly = True
                .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            End With
            '/ Index = 2
            With ProductName
                .Name = "ProductName"
                .HeaderText = "Product Name"
                .ReadOnly = True
                .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            End With
            '/ Index = 3
            With Quantity
                .Name = "Quantity"
                .HeaderText = "Quantity"
                .ValueType = GetType(Integer)
            End With
            '/ Index = 4
            With UnitPrice
                .Name = "UnitPrice"
                .HeaderText = "Unit Price"
                .ValueType = GetType(Double)
            End With
            '/ Index = 5
            With Total
                .Name = "Total"
                .HeaderText = "Total"
                .ValueType = GetType(Double)
                .ReadOnly = True
                .ReadOnly = True
                .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
                .DefaultCellStyle.ForeColor = Color.Blue
                .DefaultCellStyle.Font = New Font(dgvData.Font, FontStyle.Bold)
            End With
            '// เพิ่มปุ่มลบ (Index = 6)
            Dim btnDelRow As New DataGridViewButtonColumn
            dgvData.Columns.Add(btnDelRow)
            With btnDelRow
                .HeaderText = "Delete F8"
                .Text = "Delete"
                .UseColumnTextForButtonValue = True
                .Width = 30
                .ReadOnly = True
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable'/ Not sort order but can click header for delete row.
            End With
            '/ Alignment MiddleRight only columns 3 to 5
            For i As Byte = 3 To 5
                '/ Header Alignment
                .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                '/ Cell Alignment
                .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            Next
            '/ Auto size column width of each main by sorting the field.
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            '/ Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.RoyalBlue
                .ForeColor = Color.White
                .Font = New Font("Tahoma", 11, FontStyle.Bold)
            End With
            .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
            .ColumnHeadersHeight = 36
            '/ กำหนดให้ EnableHeadersVisualStyles = False เพื่อให้ยอมรับการเปลี่ยนแปลงสีพื้นหลังของ Header
            .EnableHeadersVisualStyles = False
      End With
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / การค้นหาข้อมูลในช่อง TextBox และค้นหาว่ามีข้อมูลรายการสินค้าอยู่ในแถวรายการตารางกริดหรือไม่
    ' / หากไม่มี ก็ให้เพิ่มแถวเข้าไปใหม่
    ' / หากมี ก็ให้เพิ่มจำนวนขึ้นไปอีก 1 Unit.
    ' / --------------------------------------------------------------------------------
    Private Sub txtSearch_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
      '// เมื่อกดคีย์ ENTER เพื่อเริ่มต้นการค้นหาข้อมูล
      If e.KeyChar = Chr(13) Then
            '/ Replace some word for reserved in DataBase.
            txtSearch.Text = txtSearch.Text.Trim.Replace("'", "").Replace("*", "").Replace("%", "")
            e.Handled = True    '// ปิดเสียง
            '/ สร้าง DataTable สมมุติขึ้นมา (เหมือนกับการดึงข้อมูลจาก DataBase เข้ามาเพื่อทำการค้นหา)
            Dim DT As DataTable = GetDataTable()
            '/ ค้นหาข้อมูลจาก DataTable แล้วรับค่ามาใส่ไว้ใน DataRow
            '/ การค้นหาข้อมูลแบบ String จะต้องใส่เครื่องหมาย Single Quote ครอบเอาไว้ เช่น ProductID = '01'
            Dim r() As DataRow = DT.Select(" ProductID = " & "'" & txtSearch.Text.Trim & "'")
            '// หากพบข้อมูลใน DataTable
            If r.Count > 0 Then
                '/ ตัวแปรบูลีน Flag แจ้งการค้นหาข้อมูลในตารางกริด (True = พบรายการในแถว, False = ไม่พบ)
                Dim blnExist As Boolean = False
                '/ หากนับจำนวนรวมสินค้าชนิดเดียวกันใน 1 รายการ (ไม่ติ๊กเครื่องหมายถูกใน CheckBox)
                '/ หากต้องการให้นับจำนวนรวมอย่างเดียวเท่านั้น ก็ตัดในส่วน IF - END IF และ CheckBox ทิ้งออกไปได้เลย
                If Not chkSeparateQty.Checked Then
                  '/ ต้องค้นหาข้อมูลจากตารางกริดก่อน เพื่อค้นหาว่ามีรายการสินค้าเดิมหรือไม่?
                  '/ หากในตารางกริดยังไม่มีแถวรายการ ก็จะข้าม For Loop นี้ไปเพิ่มรายการใหม่ทันที
                  For iRow As Integer = 0 To dgvData.RowCount - 1
                        '/ ทดสอบด้วย Primary Key r(0).Item(0) หรือ Product ID r(0).Item(1) ก็ได้
                        If r(0).Item(0) = dgvData.Rows(iRow).Cells(0).Value Then
                            '/ เมื่อพบรายการเดิม ก็ให้เพิ่มจำนวนขึ้น 1
                            dgvData.Rows(iRow).Cells(3).Value += 1
                            '/ Flag แจ้งว่าพบข้อมูลเดิมแล้ว
                            blnExist = True
                            '/ เมื่อเจอสินค้าเดิมในตารางกริดแล้ว ไม่ว่าจะอยู่แถวลำดับที่เท่าไหร่ ก็ให้ออกจาก For Loop การค้นหาได้เลย
                            '/ เพราะรายการสินค้าใดๆ จะต้องมีอยู่เพียงแค่รายการเดียว ไม่ต้องเสียเวลาวนรอบกลับไปทำให้จนครบจำนวนแถว
                            Exit For
                        End If
                  Next
                End If
                '/ กรณีที่พบสินค้าในตารางกริด กำหนด blnExist = True ทำให้ Not True = False จะทำให้ข้ามเงื่อนไขนี้ออกไป
                '/ กรณีที่ไม่พบข้อมูลสินค้าเดิมในตารางกริด กำหนด blnExist = False ทำให้ Not False = True เพิ่มรายการสินค้าแถวใหม่เข้าไปในตารางกริดได้
                If Not blnExist Then
                  '/ เขียนเอาไว้หน่อยจะได้ไม่ลืมว่าแต่ละหลักเราเก็บค่าอะไร
                  '/ Primary Key, Product ID, Product Name, Quantity, UnitPrice, Total
                  dgvData.Rows.Add(r(0).Item(0), r(0).Item(1), r(0).Item(2), "1", Format(CDbl(r(0).Item(3).ToString), "0.00"), "0.00")
                End If
                '/ หากไม่ใช้ NOT ก็จะต้องเขียนโปรแกรมแบบนี้ แต่โค้ดจะไม่สวยงาม
                '/ If blnExist = True Then
                '/   ไม่ต้องทำอะไร
                '/ Else
                '/   ทำคำสั่งเพิ่มรายการแถว
                '/ End If
                '// คำนวณผลรวมใหม่
                DT.Dispose()
                Call CalSumTotal()
            End If
            txtSearch.Clear()
            txtSearch.Focus()
      End If
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Calcualte sum of Total (Column Index = 5)
    ' / ทำทุกครั้งที่มีการเพิ่ม/ลบแถวรายการ หรือมีการเปลี่ยนแปลงค่าในเซลล์ Quantity, UnitPrice
    ' / เพื่อคำนวณหาจำนวนเงินใหม่
    Private Sub CalSumTotal()
      txtTotal.Text = "0.00"
      '/ วนรอบตามจำนวนแถวที่มีอยู่ปัจจุบัน
      For i As Integer = 0 To dgvData.RowCount - 1
            '/ หลักสุดท้ายของตารางกริด = [จำนวน x ราคา]
            dgvData.Rows(i).Cells(5).Value = Format(dgvData.Rows(i).Cells(3).Value * dgvData.Rows(i).Cells(4).Value, "#,##0.00")
            '/ นำค่าจาก Total มารวมกันเพื่อแสดงผลในสรุปผลรวม (x = x + y)
            txtTotal.Text = Format(CDbl(txtTotal.Text) + CDbl(dgvData.Rows(i).Cells(5).Value), "#,##0.00")
      Next
      '// TAX - การคิดภาษี
      Dim SumPrice As Double = CDbl(txtTotal.Text)
      Select Case cmbTax.SelectedIndex
            '// ไม่คิดภาษี
            Case 0
                txtTotal.Text = Format(SumPrice, "#,##0.00")
                txtVat.Text = "0.00"
                '// รวมจำนวนเงินทั้งหมด
                txtNetTotal.Text = Format(CDbl(txtTotal.Text), "#,##0.00")
                '// หรือ txtNetTotal.Text = Format(SumPrice, "#,##0.00")

                '// รวมภาษี (Include Tax)
            Case 1
                '// คิดภาษี 7% (Include VAT)
                txtVat.Text = Format(SumPrice - (SumPrice / 1.07), "#,##0.00")
                '// หาราคาสินค้าที่แท้จริงก่อน ... โดยเอาราคาสินค้าทั้งหมดลบออกจากภาษี
                txtTotal.Text = Format(SumPrice - CDbl(txtVat.Text), "#,##0.00")
                '// รวมจำนวนเงินทั้งหมด
                txtNetTotal.Text = Format(SumPrice, "#,##0.00")

                '// แยกภาษี (Exclude Tax)
            Case 2
                txtTotal.Text = Format(SumPrice, "#,##0.00")
                '// คิดแยกภาษี 7% (Exclude VAT)
                txtVat.Text = Format(CDbl(txtTotal.Text) * 7 / 100, "#,##0.00")
                '// รวมจำนวนเงินทั้งหมด + ภาษี
                txtNetTotal.Text = Format(CDbl(txtTotal.Text) + CDbl(txtVat.Text), "#,##0.00")
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / โปรแกรมย่อยในการลบแถวรายการที่เลือกออกไป
    ' / --------------------------------------------------------------------------------
    Private Sub DeleteRow(ByVal ColName As String)
      If dgvData.RowCount = 0 Then Return
      '/ ColName เป็นชื่อของหลัก Index = 6 ของตารางกริด (ไปดูที่โปรแกรมย่อย InitializeGrid)
      If ColName = "btnDelRow" Then
            '// ลบรายการแถวที่เลือกออกไป
            dgvData.Rows.Remove(dgvData.CurrentRow)
            '/ เมื่อแถวรายการถูกลบออกไป ต้องไปคำนวณหาค่าผลรวมใหม่
            Call CalSumTotal()
      End If
      txtSearch.Focus()
    End Sub

    Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
      Select Case e.ColumnIndex
            '// Delete Button
            Case 6
                'MsgBox(("Row : " + e.RowIndex.ToString & "Col : ") + e.ColumnIndex.ToString)
                Call DeleteRow("btnDelRow")
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / เหตุการณ์นี้จะเกิดขึ้นเมื่อแก้ไขรายการในเซลล์เรียบร้อยแล้วกด Enter
    ' / --------------------------------------------------------------------------------
    Private Sub dgvData_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
      '/ เกิดการเปลี่ยนแปลงค่าในหลัก Index ที่ 3 หรือ 4
      '/ ให้คำนวณราคารวมใหม่ จำนวน X ราคา
      Select Case e.ColumnIndex
            '/ Column Index = 3 (Quantity), Column Index = 4 (UnitPrice)
            Case 3, 4
                '/ Quantity
                '/ การดัก Error กรณีมีค่า Null Value ให้ใส่ค่า 0 ลงไปแทน
                If IsDBNull(dgvData.Rows(e.RowIndex).Cells(3).Value) Then dgvData.Rows(e.RowIndex).Cells(3).Value = "0"
                Dim Quantity As Integer = dgvData.Rows(e.RowIndex).Cells(3).Value
                '/ UnitPrice
                '/ If Null Value
                If IsDBNull(dgvData.Rows(e.RowIndex).Cells(4).Value) Then dgvData.Rows(e.RowIndex).Cells(4).Value = "0.00"
                Dim UnitPrice As Double = dgvData.Rows(e.RowIndex).Cells(4).Value
                dgvData.Rows(e.RowIndex).Cells(4).Value = Format(CDbl(dgvData.Rows(e.RowIndex).Cells(4).Value), "0.00")

                '/ Quantity x UnitPrice
                dgvData.Rows(e.RowIndex).Cells(5).Value = CDbl((Quantity * UnitPrice).ToString("#,##0.00"))
                '/ Calculate Summary
                Call CalSumTotal()
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / เมื่อเริ่มกดคีย์ในหลักที่ 3 และ 4 (Quantity, UnitPrice)
    ' / --------------------------------------------------------------------------------
    Private Sub dgvData_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvData.EditingControlShowing
      '// อย่าลืมกำหนดชื่อ Column Name มาก่อนหน้านี้ด้วย (โปรแกรมย่อย InitializeGrid)
      Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Name
            ' / Can use both Colume Index or Field Name
            Case "Quantity", "UnitPrice"
                '/ Stop and Start event handler.
                RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
                AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / ดักค่าการกดคีย์เฉพาะตัวเลขเท่านั้น
    ' / --------------------------------------------------------------------------------
    Private Sub ValidKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
      Dim tb As TextBox = sender
      Select Case dgvData.CurrentCell.ColumnIndex
            Case 3' Quantity is Integer (กรณีเลขจำนวนเต็ม)
                Select Case e.KeyChar
                  Case "0" To "9"   ' digits 0 - 9 allowed
                  Case ChrW(Keys.Back)    ' backspace allowed for deleting (Delete key automatically overrides)

                  Case Else ' everything else ....
                        ' True = CPU cancel the KeyPress event
                        e.Handled = True ' and it's just like you never pressed a key at all
                End Select

            Case 4' UnitPrice is Double (กรณีเลขจำนวนทศนิยม)
                Select Case e.KeyChar
                  Case "0" To "9"
                        ' Allowed "."
                  Case "."
                        ' can present "." only one
                        If InStr(tb.Text, ".") Then e.Handled = True

                  Case ChrW(Keys.Back)
                        '/ Return False is Default value

                  Case Else
                        e.Handled = True

                End Select
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / เลือกการคำนวณภาษี
    ' / --------------------------------------------------------------------------------
    Private Sub cmbTax_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbTax.SelectedIndexChanged
      Call CalSumTotal()
      txtSearch.Focus()
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / หากติ๊กเครื่องหมายถูกคือการขายแบบแยกจำนวนสินค้า
    ' / --------------------------------------------------------------------------------
    Private Sub chkSeparateQty_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkSeparateQty.CheckedChanged
      If chkSeparateQty.Checked Then
            '// ปรับจำนวนให้เหลือ 1
            'For i As Integer = 0 To dgvData.RowCount - 1
            'dgvData.Rows(i).Cells(3).Value = "1"
            'Next
            '// หรือลบรายการทั้งหมดออกไปก่อน
            dgvData.Rows.Clear()
            Call CalSumTotal()
            dgvData.Columns("Quantity").ReadOnly = True
            dgvData.Columns("Quantity").DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
      Else
            '// ลบรายการทั้งหมดออกไปก่อน
            dgvData.Rows.Clear()
            '/ สามารถคีย์จำนวนรายการได้
            dgvData.Columns("Quantity").ReadOnly = False
            dgvData.Columns("Quantity").DefaultCellStyle.BackColor = Color.White
      End If
      txtSearch.Focus()
    End Sub

    Private Sub frmPosDetail_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      GC.SuppressFinalize(Me)
      Application.Exit()
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] โค้ดในส่วนของรายละเอียดการขายสินค้า แบบไม่ต้องติดต่อกับฐานข้อมูล