[VB.NET] ฟอร์มการชำระเงินด้วยการสร้างปุ่มคำสั่ง (Button) แบบไดนามิค
http://www.g2gnet.com/webboard/images/vbnet/paymentform.pngอย่างที่เรียนให้ทราบมาก่อนแล้วว่า 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 ที่ถูกสร้างขึ้นมาใหม่ ให้มาอยู่ที่เดียวกันหมดทุกๆตัว ...
มาดูโค้ดกันเถอะ ...
#Region "ABOUT"
' / ---------------------------------------------------------------
' / 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.
' / ---------------------------------------------------------------
#End Region
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
'// เพิ่มปุ่มคำสั่ง 3 หลัก จำนวน 9 ตัว
Call AddButtons(3, 9)
'//
Dim Rnd As New Random
txtAmount.Text = Format(CDbl(Rnd.Next(1000, 9999)), "#,##")
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)
Me.Panel1.Width = ColCount * 100
For i As Integer = 0 To MaxBtn - 1
Dim B As New Button
'// นำปุ่มไปใส่ไว้ใน Panel
Me.Panel1.Controls.Add(B)
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 เอาไว้เก็บค่าอย่างอื่นก็ได้
.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 = Val(txtCash.Text + btn.Text)
'// คำนวณผลรวม
Call CalSum()
txtCash.Focus()
End Sub
' / ---------------------------------------------------------------
' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น (ควรนำไปเก็บไว้ใน Module)
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
' / ---------------------------------------------------------------
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
'// CDbl = Convert to Double, หากใช้แค่เลขจำนวนเต็ม สามารถใช้การ Convert เป็นแบบเลขจำนวนเต็มแทน เช่น CInt หรือ CLng
If CDbl(txtCash.Text) - CDbl(txtAmount.Text) < 0 Then
MessageBox.Show("จำนวนเงินยังไม่ครบ กรุณาแก้ไขข้อมูลใหม่ด้วย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtCash.Focus()
Else
MessageBox.Show("ทำรายการเสร็จสิ้น.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim Rnd As New Random
txtAmount.Text = Format(CDbl(Rnd.Next(1000, 9999)), "#,##")
txtCash.Text = 0
txtChange.Text = "0.00"
'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
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
หน้า:
[1]