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

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

[VB.NET] ฟอร์มการชำระเงินด้วยการสร้างปุ่มคำสั่ง (Button) แบบไดนามิค

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

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5971



อย่างที่เรียนให้ทราบมาก่อนแล้วว่า Control ใน VB.NET จะไม่มี Index หรือ Array ซึ่งต่างไปจากเดิมที่มีอยู่ใน VB6 ดังนั้นจึงทำให้เกิดเหตุการณ์ซ้ำๆขึ้นมา เราจึงต้องใช้ List ของ Control เข้ามาเพื่อช่วยแก้ปัญหา เพื่อรวบเหตุการณ์ในการกดคลิ๊กเมาส์ที่ปุ่มคำสั่งมาไว้ที่เดียวกัน (อ่านรายละเอียด
การสร้างเหตุการณ์เดียวกันให้กับปุ่มคำสั่ง (Button) แบบหลายตัว) สำหรับบทความนี้แอดมินจะ ทำการสร้างปุ่มคำสั่งในแบบไดนามิค หรือ Run Time ด้วยการใช้ Dictionary object ซึ่งเป็น การจับคู่ระหว่างค่า Text ของ Button Control และตัวปุ่มของมันเอง ซึ่งก็จะคล้ายๆกับ Array นั่นเอง ด้วยการประกาศตัวแปร Dim Buttons As New Dictionary(Of String, Button) ซึ่งจะทำให้ปุ่มคำสั่งถูกบังคับด้วยเหตุการณ์ ClickButton ที่ถูกสร้างขึ้นมาใหม่ ให้มาอยู่ที่เดียวกันหมดทุกๆตัว ...

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

  17. Public Class frmPayment

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

  26.     Private Sub frmPayment_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  27.         '// เพิ่มปุ่มคำสั่ง 3 หลัก จำนวน 9 ตัว
  28.         Call AddButtons(3, 9)
  29.         '//
  30.         Dim Rnd As New Random
  31.         txtAmount.Text = Format(CDbl(Rnd.Next(1000, 9999)), "#,##")
  32.         txtCash.Text = "0"
  33.         txtChange.Text = "0.00"
  34.         '//
  35.         txtCash.Focus()
  36.     End Sub

  37.     '// เพิ่มปุ่มคำสั่ง (Button Control) รับค่าจำนวนหลัก, จำนวนปุ่มสูงสุด
  38.     Private Sub AddButtons(ByVal ColCount As Byte, ByVal MaxBtn As Byte)
  39.         Dim Buttons As New Dictionary(Of String, Button)
  40.         Me.Panel1.Width = ColCount * 100
  41.         For i As Integer = 0 To MaxBtn - 1
  42.             Dim B As New Button
  43.             '// นำปุ่มไปใส่ไว้ใน Panel
  44.             Me.Panel1.Controls.Add(B)
  45.             With B
  46.                 .Height = 60
  47.                 .Width = 66
  48.                 .Left = (i Mod ColCount) * B.Width
  49.                 .Top = (i \ ColCount) * B.Height
  50.                 .Text = i + 1
  51.                 Buttons.Add(B.Text, B)
  52.                 '// Tag เอาไว้เก็บค่าอย่างอื่นก็ได้
  53.                 .Tag = i + 1
  54.                 .Cursor = Cursors.Hand
  55.                 .Font = New Font("Tahoma", 14, FontStyle.Bold)
  56.             End With
  57.             '// Force events handler.
  58.             AddHandler B.Click, AddressOf ClickButton
  59.         Next
  60.     End Sub

  61.     '// Click Button event, get the text of button
  62.     Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs)
  63.         Dim btn As Button = sender
  64.         'MessageBox.Show("คุณคลิ๊ก [" + btn.Text + "]" & vbCrLf & "Tag=" & btn.Tag)
  65.         '// เอาตัวอักขระมาเรียงต่อกันก่อน แล้วค่อยแปลงเป็นตัวเลข
  66.         txtCash.Text = Val(txtCash.Text + btn.Text)
  67.         '// คำนวณผลรวม
  68.         Call CalSum()
  69.         txtCash.Focus()
  70.     End Sub

  71.     ' / ---------------------------------------------------------------
  72.     ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น (ควรนำไปเก็บไว้ใน Module)
  73.     Function CheckDigitOnly(ByVal index As Integer) As Boolean
  74.         Select Case index
  75.             Case 48 To 57 ' เลข 0 - 9
  76.                 CheckDigitOnly = False
  77.             Case 8, 13 ' Backspace = 8, Enter = 13
  78.                 CheckDigitOnly = False
  79.             Case Else
  80.                 CheckDigitOnly = True
  81.         End Select
  82.     End Function

  83.     ' / ---------------------------------------------------------------
  84.     Private Sub btnCash_Click(sender As System.Object, e As System.EventArgs) Handles btnCash.Click
  85.         If txtCash.Text = "" Or Len(txtCash.Text) = 0 Or Val(txtCash.Text) = 0 Then
  86.             txtCash.Text = 0
  87.             txtChange.Text = "0.00"
  88.             txtCash.Focus()
  89.             Exit Sub
  90.         End If
  91.         '// CDbl = Convert to Double, หากใช้แค่เลขจำนวนเต็ม สามารถใช้การ Convert เป็นแบบเลขจำนวนเต็มแทน เช่น CInt หรือ CLng
  92.         If CDbl(txtCash.Text) - CDbl(txtAmount.Text) < 0 Then
  93.             MessageBox.Show("จำนวนเงินยังไม่ครบ กรุณาแก้ไขข้อมูลใหม่ด้วย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  94.             txtCash.Focus()
  95.         Else
  96.             MessageBox.Show("ทำรายการเสร็จสิ้น.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
  97.             Dim Rnd As New Random
  98.             txtAmount.Text = Format(CDbl(Rnd.Next(1000, 9999)), "#,##")
  99.             txtCash.Text = 0
  100.             txtChange.Text = "0.00"
  101.             'Me.Close()
  102.         End If
  103.     End Sub

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

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

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

  116.     ' / ---------------------------------------------------------------
  117.     Private Sub CalSum()
  118.         '/ Trap Error
  119.         If Trim(txtCash.Text) = "" Or Len(Trim(txtCash.Text)) = 0 Then
  120.             txtCash.Text = 0
  121.             txtChange.Text = "0.00"
  122.             Exit Sub
  123.         End If
  124.         '//
  125.         Dim dChange As Double = CDbl(txtCash.Text) - CDbl(txtAmount.Text)
  126.         txtChange.Text = dChange
  127.         If CDbl(dChange) < 0 Then
  128.             txtChange.ForeColor = Color.Red
  129.         Else
  130.             txtChange.ForeColor = Color.Blue
  131.         End If
  132.         txtCash.Focus()
  133.     End Sub

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

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

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

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

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

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

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

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

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

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

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

GMT+7, 2024-3-29 02:27 , Processed in 0.197267 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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