ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3730|ตอบกลับ: 0

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

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5973



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

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

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

  27.         '// New Panel
  28.         'set the tabpage to be your desired tab
  29.         tabPageRef = TabControl1.TabPages(1)
  30.         '// Create Panel
  31.         With pn2
  32.             .Location = New System.Drawing.Point(1, 1)
  33.             .Size = New System.Drawing.Size(TabControl1.Width - 10, TabControl1.Height - 30)
  34.             .BackColor = Color.Moccasin
  35.             .AutoScroll = True
  36.             .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
  37.         End With
  38.         'add the panel
  39.         tabPageRef.Controls.Add(pn2)
  40.         strSQL = _
  41.             " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
  42.             " FROM(Food) " & _
  43.             " WHERE Food.CategoryFK = 3 " & _
  44.             " ORDER BY FoodPK "
  45.         '// สร้างปุ่ม (Button) ลงใน Panel (จำนวนหลัก, รายการ, Panel)
  46.         Call AddButtonsToForm(3, strSQL, pn2)
คัดลอกไปที่คลิปบอร์ด

การสร้างปุ่มคำสั่งลงใน TabControl และ Panel ...    
  1.     ' / ---------------------------------------------------------------
  2.     '// เพิ่มปุ่มคำสั่ง (Button Control) แบบ Run Time
  3.     Private Sub AddButtonsToForm(ByVal ColCount As Byte, ByVal sql As String, pn As Panel)
  4.         Dim Buttons As New Dictionary(Of String, Button)
  5.         Dim img As String
  6.         Dim Rec As Integer = 0
  7.         Try
  8.             If Conn.State = ConnectionState.Closed Then Conn.Open()
  9.             Cmd = New OleDbCommand(sql, Conn)
  10.             DR = Cmd.ExecuteReader
  11.             ' / Make sure Primary Key only one and not duplicate
  12.             While DR.Read()
  13.                 If DR.HasRows Then
  14.                     Dim B As New Button
  15.                     pn.Controls.Add(B)
  16.                     With B
  17.                         .Height = 140
  18.                         .Width = 140
  19.                         .Left = (Rec Mod ColCount) * B.Width
  20.                         .Top = (Rec \ ColCount) * B.Height
  21.                         .Text = DR.Item("FoodName") & vbCrLf & Format(CDbl(DR.Item("PriceCash")), "#,##0.00") & "B."
  22.                         Buttons.Add(B.Text, B)
  23.                         '// นำค่า Primary Key ไปเก็บไว้ที่คุณสมบัติ Tag เมื่อกดปุ่มคำสั่งจะใช้ค่านี้ไปค้นหาจากฐานข้อมูล
  24.                         .Tag = DR.Item("FoodPK")
  25.                         '// อ่านค่ารูปภาพ และตรวจสอบการมีอยู่จริงของภาพด้วยฟังค์ชั่น GetImages
  26.                         img = GetImages(DR.Item("PictureFood"))
  27.                         '// ใส่ภาพลงไปในปุ่มคำสั่ง Button
  28.                         .BackgroundImage = New System.Drawing.Bitmap(img)
  29.                         '//
  30.                         .Cursor = Cursors.Hand
  31.                         .BackgroundImageLayout = ImageLayout.Stretch
  32.                         .Font = New Font("Century Gothic", 14, FontStyle.Bold)
  33.                         .ForeColor = Color.LightYellow
  34.                         .TextImageRelation = TextImageRelation.ImageAboveText
  35.                         .TextAlign = ContentAlignment.BottomCenter
  36.                         .UseVisualStyleBackColor = True
  37.                     End With
  38.                     Rec += 1
  39.                     '// Force events handler.
  40.                     AddHandler B.Click, AddressOf ClickButton
  41.                 End If
  42.             End While
  43.             DR.Close()
  44.         Catch ex As Exception
  45.             MessageBox.Show(ex.Message)
  46.         End Try

  47.     End Sub
คัดลอกไปที่คลิปบอร์ด

โค้ดในการคลิ๊กที่ปุ่มคำสั่งเลือกรายการอาหาร/เครื่องดื่ม หากมีรายการเดิมจะทำการเพิ่มจำนวน หากไม่มีต้องค้นรายการมาเพิ่มลงในตารางกริด
  1.     ' / ---------------------------------------------------------------
  2.     ' / เพิ่มรายการแถวใหม่ (UnBound Data)
  3.     Private Sub AddRow(ByVal PK As Integer) ', ByVal Description As String, ByVal UnitPrice As Double)
  4.         '// ใช้เพื่อค้นหาข้อมูลในตารางกริดก่อน
  5.         Dim blnExist As Boolean = False
  6.         '// ตรวจสอบว่า Primary Key มีอยู่ในตารางกริดหรือไม่ (หากมีให้ +เพิ่ม, หากไม่มีค่อยไปค้นหาข้อมูล)
  7.         For Rec As Integer = 0 To dgvData.RowCount - 1
  8.             ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
  9.             If dgvData.Rows(Rec).Cells(0).Value = PK Then
  10.                 ' / ให้บวกจำนวนที่เลือกเพิ่มขึ้นอีก 1 (Quantity = Quantity + 1)
  11.                 dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value + 1
  12.                 lblLastPrice.Text = "Last Price: " & Format(CDbl(dgvData.Rows(Rec).Cells(2).Value), "#,##0.00")
  13.                 '// เจอข้อมูลเดิม
  14.                 blnExist = True
  15.                 '// ออกจากลูปไปเลย เพื่อไม่ให้เสียเวลา
  16.                 Exit For
  17.             End If
  18.         Next
  19.         '// ไม่พบข้อมูลในตารางกริด ก็ทำการเรียกจากตารางข้อมูลเข้ามาแสดงผล
  20.         If Not blnExist Then
  21.             If Conn.State = ConnectionState.Closed Then Conn.Open()
  22.             strSQL = _
  23.                 " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
  24.                 " FROM(Food) " & _
  25.                 " WHERE Food.FoodPK = " & PK & _
  26.                 " ORDER BY FoodPK "
  27.             Cmd = New OleDbCommand
  28.             Cmd.Connection = Conn
  29.             Cmd.CommandText = strSQL
  30.             DR = Cmd.ExecuteReader
  31.             DR.Read()
  32.             '// เพิ่มรายการแถวสินค้าเข้าไปใหม่ Primary Key (ถูกซ่อน), ชื่อสินค้า, ราคาขาย, จำนวน = 1
  33.             Dim row As String() = New String() {PK, DR.Item("FoodName").ToString, Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00"), "1"}
  34.             dgvData.Rows.Add(row)
  35.         End If
  36.         '// หาจำนวนเงินรวมใหม่
  37.         Call CalAmount()
  38.         '// เป็นสินค้าตัวใหม่ที่ถูกเพิ่มเข้ามาให้อยู่ในแถว แสดงราคาสินค้าล่าสุด
  39.         If Not blnExist Then
  40.             lblLastPrice.Text = "Last Price: " & Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00")
  41.         End If
  42.         DR.Close()
  43.         '// โฟกัสไปที่ DataGridView แล้วย้ายไปแถวล่าสุด
  44.         dgvData.Focus()
  45.         SendKeys.Send("^{END}")
  46.     End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดฉบับเต็มกันเถอะ ... (ฟอร์มหลัก frmMainOrder.vb)
  1. ' / ---------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / More Info: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Prototype of Burger Order System V2.
  10. ' / Microsoft Visual Basic .NET (2010) + MS Access 2007+
  11. ' /
  12. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / ---------------------------------------------------------------
  15. Imports System.Data.OleDb

  16. Public Class frmMainOrder

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

  21.     Private Sub frmMainOrder_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  22.         Select Case e.KeyCode
  23.             Case Keys.F8
  24.                 btnPayment_Click(sender, e)
  25.             Case Keys.F10
  26.                 Me.Close()
  27.         End Select
  28.     End Sub

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

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

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

  82.     End Sub

  83.     ' / ---------------------------------------------------------------
  84.     '// เพิ่มปุ่มคำสั่ง (Button Control) แบบ Run Time
  85.     Private Sub AddButtonsToForm(ByVal ColCount As Byte, ByVal sql As String, pn As Panel)
  86.         Dim Buttons As New Dictionary(Of String, Button)
  87.         Dim img As String
  88.         Dim Rec As Integer = 0
  89.         Try
  90.             If Conn.State = ConnectionState.Closed Then Conn.Open()
  91.             Cmd = New OleDbCommand(sql, Conn)
  92.             DR = Cmd.ExecuteReader
  93.             ' / Make sure Primary Key only one and not duplicate
  94.             While DR.Read()
  95.                 If DR.HasRows Then
  96.                     Dim B As New Button
  97.                     pn.Controls.Add(B)
  98.                     With B
  99.                         .Height = 140
  100.                         .Width = 140
  101.                         .Left = (Rec Mod ColCount) * B.Width
  102.                         .Top = (Rec \ ColCount) * B.Height
  103.                         .Text = DR.Item("FoodName") & vbCrLf & Format(CDbl(DR.Item("PriceCash")), "#,##0.00") & "B."
  104.                         Buttons.Add(B.Text, B)
  105.                         '// นำค่า Primary Key ไปเก็บไว้ที่คุณสมบัติ Tag เมื่อกดปุ่มคำสั่งจะใช้ค่านี้ไปค้นหาจากฐานข้อมูล
  106.                         .Tag = DR.Item("FoodPK")
  107.                         '// อ่านค่ารูปภาพ และตรวจสอบการมีอยู่จริงของภาพด้วยฟังค์ชั่น GetImages
  108.                         img = GetImages(DR.Item("PictureFood"))
  109.                         '// ใส่ภาพลงไปในปุ่มคำสั่ง Button
  110.                         .BackgroundImage = New System.Drawing.Bitmap(img)
  111.                         '//
  112.                         .Cursor = Cursors.Hand
  113.                         .BackgroundImageLayout = ImageLayout.Stretch
  114.                         .Font = New Font("Century Gothic", 14, FontStyle.Bold)
  115.                         .ForeColor = Color.LightYellow
  116.                         .TextImageRelation = TextImageRelation.ImageAboveText
  117.                         .TextAlign = ContentAlignment.BottomCenter
  118.                         .UseVisualStyleBackColor = True
  119.                     End With
  120.                     Rec += 1
  121.                     '// Force events handler.
  122.                     AddHandler B.Click, AddressOf ClickButton
  123.                 End If
  124.             End While
  125.             DR.Close()
  126.         Catch ex As Exception
  127.             MessageBox.Show(ex.Message)
  128.         End Try

  129.     End Sub

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

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

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

  150.     ' / ---------------------------------------------------------------
  151.     ' / เพิ่มรายการแถวใหม่ (UnBound Data)
  152.     Private Sub AddRow(ByVal PK As Integer) ', ByVal Description As String, ByVal UnitPrice As Double)
  153.         '// ใช้เพื่อค้นหาข้อมูลในตารางกริดก่อน
  154.         Dim blnExist As Boolean = False
  155.         '// ตรวจสอบว่า Primary Key มีอยู่ในตารางกริดหรือไม่ (หากมีให้ +เพิ่ม, หากไม่มีค่อยไปค้นหาข้อมูล)
  156.         For Rec As Integer = 0 To dgvData.RowCount - 1
  157.             ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
  158.             If dgvData.Rows(Rec).Cells(0).Value = PK Then
  159.                 ' / ให้บวกจำนวนที่เลือกเพิ่มขึ้นอีก 1 (Quantity = Quantity + 1)
  160.                 dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value + 1
  161.                 lblLastPrice.Text = "Last Price: " & Format(CDbl(dgvData.Rows(Rec).Cells(2).Value), "#,##0.00")
  162.                 '// เจอข้อมูลเดิม
  163.                 blnExist = True
  164.                 '// ออกจากลูปไปเลย เพื่อไม่ให้เสียเวลา
  165.                 Exit For
  166.             End If
  167.         Next
  168.         '// ไม่พบข้อมูลในตารางกริด ก็ทำการเรียกจากตารางข้อมูลเข้ามาแสดงผล
  169.         If Not blnExist Then
  170.             If Conn.State = ConnectionState.Closed Then Conn.Open()
  171.             strSQL = _
  172.                 " SELECT Food.FoodPK, Food.FoodID, Food.FoodName, Food.PriceCash, Food.PictureFood, Food.CategoryFK" & _
  173.                 " FROM(Food) " & _
  174.                 " WHERE Food.FoodPK = " & PK & _
  175.                 " ORDER BY FoodPK "
  176.             Cmd = New OleDbCommand
  177.             Cmd.Connection = Conn
  178.             Cmd.CommandText = strSQL
  179.             DR = Cmd.ExecuteReader
  180.             DR.Read()
  181.             '// เพิ่มรายการแถวสินค้าเข้าไปใหม่ Primary Key (ถูกซ่อน), ชื่อสินค้า, ราคาขาย, จำนวน = 1
  182.             Dim row As String() = New String() {PK, DR.Item("FoodName").ToString, Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00"), "1"}
  183.             dgvData.Rows.Add(row)
  184.         End If
  185.         '// หาจำนวนเงินรวมใหม่
  186.         Call CalAmount()
  187.         '// เป็นสินค้าตัวใหม่ที่ถูกเพิ่มเข้ามาให้อยู่ในแถว แสดงราคาสินค้าล่าสุด
  188.         If Not blnExist Then
  189.             lblLastPrice.Text = "Last Price: " & Format(CDbl(DR.Item("PriceCash").ToString), "#,##0.00")
  190.         End If
  191.         DR.Close()
  192.         '// โฟกัสไปที่ DataGridView แล้วย้ายไปแถวล่าสุด
  193.         dgvData.Focus()
  194.         SendKeys.Send("^{END}")
  195.     End Sub

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

  205.     ' / ---------------------------------------------------------------
  206.     '// Initialize DataGridView @Run Time
  207.     Private Sub InitGrid()
  208.         '// ตั้งค่าจำนวนหลัก (Columns)
  209.         With dgvData
  210.             .Columns.Add("FoodPK", "FoodPK")
  211.             .Columns.Add("FoodName", "รายการ")
  212.             .Columns.Add("PriceCash", "ราคา")
  213.             .Columns.Add("Quantity", "จำนวน")
  214.             '//
  215.             .Columns(0).Visible = False
  216.             '.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
  217.             '// UnitPrice
  218.             With .Columns(2)
  219.                 .ValueType = GetType(Decimal)
  220.                 .DefaultCellStyle.Format = "N2"
  221.                 .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  222.             End With
  223.             '// Quantity
  224.             With .Columns(3)
  225.                 .ValueType = GetType(Double)
  226.                 .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  227.             End With
  228.         End With
  229.         '//
  230.         With dgvData
  231.             .RowHeadersVisible = False
  232.             .AllowUserToAddRows = False
  233.             .AllowUserToDeleteRows = False
  234.             .AllowUserToResizeRows = False
  235.             .MultiSelect = False
  236.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  237.             .ReadOnly = True
  238.             '//
  239.             .Font = New Font("Century Gothic", 14)
  240.             .RowTemplate.Height = 40
  241.             '.RowTemplate.MinimumHeight = 32
  242.             ' Autosize Column
  243.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  244.             .AutoResizeColumns()
  245.             '// Even-Odd Color
  246.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  247.             ' Adjust Header Styles
  248.             With .ColumnHeadersDefaultCellStyle
  249.                 .BackColor = Color.Navy
  250.                 .ForeColor = Color.Black
  251.                 .Font = New Font("Century Gothic", 16, FontStyle.Bold)
  252.                 .WrapMode = DataGridViewTriState.False
  253.             End With
  254.         End With

  255.     End Sub

  256.     ' / ---------------------------------------------------------------
  257.     Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
  258.         If dgvData.RowCount <= 0 Then Exit Sub
  259.         dgvData.Focus()
  260.         For Rec As Integer = 0 To dgvData.RowCount - 1
  261.             '// ใช้หลัก PK เป็นกุญแจหลักในการอ้างอิง
  262.             Dim PK As Integer = dgvData.CurrentRow.Cells(0).Value
  263.             ' / หากพบ Primary Key ในหลัก 0 มาตรงกันกับค่าในแถวของตารางกริด
  264.             If dgvData.Rows(Rec).Cells(0).Value = PK Then
  265.                 '// หากพบข้อมูลเดิม แต่มีจำนวน = 1 ก็ลบออกไปได้เลย
  266.                 If dgvData.Rows(Rec).Cells(3).Value = 1 Then
  267.                     dgvData.Rows.Remove(dgvData.CurrentRow)
  268.                     dgvData.Refresh()
  269.                     '// ไม่ต้องไปลบอีกรอบ
  270.                     Exit For
  271.                 Else
  272.                     ' / ให้ลบจำนวนลง 1 (Quantity = Quantity - 1)
  273.                     dgvData.Rows(Rec).Cells(3).Value = dgvData.Rows(Rec).Cells(3).Value - 1
  274.                     Exit For
  275.                 End If
  276.             End If
  277.         Next
  278.         '// รวมจำนวนเงิน
  279.         Call CalAmount()
  280.     End Sub

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

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

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

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

  299. End Class
คัดลอกไปที่คลิปบอร์ด

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

  16.     Private Sub frmPayment_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  17.         Select Case e.KeyCode
  18.             Case Keys.F8
  19.                 btnCash_Click(sender, e)
  20.             Case Keys.F10
  21.                 Me.Close()
  22.         End Select
  23.     End Sub

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

  34.     '// เพิ่มปุ่มคำสั่ง (Button Control)
  35.     Private Sub AddButtons(ByVal ColCount As Byte, ByVal MaxBtn As Byte)
  36.         Dim Buttons As New Dictionary(Of String, Button)
  37.         For i As Integer = 0 To MaxBtn - 1
  38.             Dim B As New Button
  39.             Me.Panel1.Controls.Add(B)
  40.             Me.Panel1.Width = ColCount * 100
  41.             With B
  42.                 .Height = 60
  43.                 .Width = 66
  44.                 .Left = (i Mod ColCount) * B.Width
  45.                 .Top = (i \ ColCount) * B.Height
  46.                 .Text = i + 1
  47.                 Buttons.Add(B.Text, B)
  48.                 .Tag = i + 1
  49.                 .Cursor = Cursors.Hand
  50.                 .Font = New Font("Tahoma", 14, FontStyle.Bold)
  51.             End With
  52.             '// Force events handler.
  53.             AddHandler B.Click, AddressOf ClickButton
  54.         Next
  55.     End Sub

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

  63.     ' / ---------------------------------------------------------------
  64.     Private Sub btnCash_Click(sender As System.Object, e As System.EventArgs) Handles btnCash.Click
  65.         If txtCash.Text = "" Or Len(txtCash.Text) = 0 Or Val(txtCash.Text) = 0 Then
  66.             txtCash.Text = 0
  67.             txtChange.Text = "0.00"
  68.             txtCash.Focus()
  69.             Exit Sub
  70.         End If
  71.         '// ตรวจสอบยอดเงิน
  72.         If CDbl(txtCash.Text) - CDbl(txtAmount.Text) < 0 Then
  73.             MessageBox.Show("จำนวนเงินยังไม่ครบ กรุณาแก้ไขข้อมูลใหม่ด้วย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  74.             txtCash.Focus()
  75.         Else
  76.             MessageBox.Show("ทำรายการเสร็จสิ้นเรียบร้อย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
  77.             '// สั่งเคลียร์รายการข้ามฟอร์มมาเลย
  78.             With frmMainOrder
  79.                 .lblAmount.Text = "0.00"
  80.                 .lblLastPrice.Text = "0.00"
  81.                 .dgvData.Rows.Clear()
  82.             End With
  83.             Me.Close()
  84.         End If
  85.     End Sub

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

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

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

  98.     ' / ---------------------------------------------------------------
  99.     Private Sub CalSum()
  100.         '/ Trap Error
  101.         If Trim(txtCash.Text) = "" Or Len(Trim(txtCash.Text)) = 0 Then
  102.             txtCash.Text = 0
  103.             txtChange.Text = "0.00"
  104.             Exit Sub
  105.         End If
  106.         '//
  107.         Dim dChange As Double = CDbl(txtCash.Text) - CDbl(txtAmount.Text)
  108.         txtChange.Text = dChange
  109.         If CDbl(dChange) < 0 Then
  110.             txtChange.ForeColor = Color.Red
  111.         Else
  112.             txtChange.ForeColor = Color.Blue
  113.         End If
  114.         txtCash.Focus()
  115.     End Sub

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

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

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

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

  130.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  131.         Me.Close()
  132.     End Sub
  133. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูลหากิน ... modDataBase.vb
  1. Imports System.Data.OleDb
  2. Module modDataBase

  3.     Public Conn As OleDbConnection
  4.     Public Cmd As OleDbCommand
  5.     Public DS As DataSet
  6.     Public DR As OleDbDataReader
  7.     Public DA As OleDbDataAdapter
  8.     Public DT As DataTable
  9.     Public strSQL As String '// Major SQL
  10.     Public strStmt As String
  11.     Public strPathImages As String = MyPath(Application.StartupPath) & "Images"

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

  17. End Module
คัดลอกไปที่คลิปบอร์ด

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

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

  15.     ' / --------------------------------------------------------------------------------
  16.     ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น
  17.     Function CheckDigitOnly(ByVal index As Integer) As Boolean
  18.         Select Case index
  19.             Case 48 To 57 ' เลข 0 - 9
  20.                 CheckDigitOnly = False
  21.             Case 8, 13 ' Backspace = 8, Enter = 13
  22.                 CheckDigitOnly = False
  23.             Case Else
  24.                 CheckDigitOnly = True
  25.         End Select
  26.     End Function
  27. End Module
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-29 14:48 , Processed in 0.145712 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้