แต่ในบางครั้งเราจำเป็นต้องสร้างออปเจกต์ แบบอัตโนมัติ พร้อมทั้งสามารถเชื่อมโยงไปยัง เหตุการณ์ (event) ที่เราต้องการ โดยผ่านคำสั่ง เช่น
AddHandler btnNew.MouseClick, AddressOf btnNew_Click
แล้วก็มีเรื่องของการอ้าง property ในแบบอาศัยตัวแปรแทนชื่อที่กำหนดไว้ เช่น
Me.Controls("Label" & RandomNumber.ToString).Enabled = False
แถมด้วย คำสั่งที่ใช้ออกเสียงภาษาอังกฤษ โดยอาศัย API ชื่อ SAPI (S น่าจะมาจาก Speech + กับ API) มาช่วย
และอื่น ๆ อีกมากมายจากโค้ดตัวอย่างต่อไปนี้ ลองทำดู แล้วจะรู้ว่าคุณทำได้
1. สร้าง Project เป็น Windows Form Application ขึ้นมา 1 ตัว ชื่อ BINGO ดังภาพด้านล่าง

2. ที่ Form1 ให้ คลิกเมาส์ที่ฟอร์ม เร็ว ๆ สองที (double-click Mouse) จะเข้าสู่หน้าต่าง เขียนคำสั่ง ให้นำคำสั่งด้านล่างนี้ไปแทนที่ได้เลย
Public Class Form1
Private lblNumber As New Label
Private lblCounter As New Label
Private btnRandom As New Button
Private btnNew As New Button
Private totLbl As Integer = 49
Private aNewlbl(totLbl) As Label
Private nTime As Integer = 0
Private nCounter As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
'Form
'
Me.Text = "BINGO"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(653, 414)
'
'lblNumber
'
lblNumber.BackColor = System.Drawing.SystemColors.ActiveCaption
lblNumber.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
lblNumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!, _
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, _
CType(222, Byte))
lblNumber.ForeColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), _
CType(CType(0, Byte), Integer), _
CType(CType(0, Byte), Integer))
lblNumber.Location = New System.Drawing.Point(10, 272)
lblNumber.Name = "lblNumber"
lblNumber.Size = New System.Drawing.Size(490, 106)
lblNumber.Text = "B I N G O"
lblNumber.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.Controls.Add(lblNumber)
'
'lblCounter
'
lblCounter.BackColor = System.Drawing.SystemColors.ActiveCaption
lblCounter.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
lblCounter.Font = New System.Drawing.Font("Microsoft Sans Serif", 36.0!, _
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, _
CType(222, Byte))
lblCounter.ForeColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), _
CType(CType(0, Byte), Integer), _
CType(CType(0, Byte), Integer))
lblCounter.Location = New System.Drawing.Point(522, 272)
lblCounter.Name = "lblCounter"
lblCounter.Size = New System.Drawing.Size(109, 63)
lblCounter.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.Controls.Add(lblCounter)
'
'btnRandom
'
btnRandom.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, _
System.Drawing.FontStyle.Regular, _
System.Drawing.GraphicsUnit.Point, _
CType(222, Byte))
btnRandom.Location = New System.Drawing.Point(508, 18)
btnRandom.Name = "btnRandom"
btnRandom.Size = New System.Drawing.Size(137, 39)
btnRandom.Text = "Random"
btnRandom.UseVisualStyleBackColor = True
AddHandler btnRandom.MouseClick, AddressOf btnRandom_Click
Me.Controls.Add(btnRandom)
'
'btnNew
'
btnNew.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, _
System.Drawing.FontStyle.Regular, _
System.Drawing.GraphicsUnit.Point, _
CType(222, Byte))
btnNew.Location = New System.Drawing.Point(508, 67)
btnNew.Name = "Button2"
btnNew.Size = New System.Drawing.Size(137, 39)
btnNew.Text = "New Game"
btnNew.UseVisualStyleBackColor = True
AddHandler btnNew.MouseClick, AddressOf btnNew_Click
Me.Controls.Add(btnNew)
'
'Label
'
Dim tt As Integer = 10
For i As Integer = 0 To totLbl
aNewlbl(i) = New Label With {.Name = "Label" & CType(i + 1, String).Trim, _
.Text = CType(i + 1, String).Trim, _
.Location = New Point(CType(((i Mod 10) * 50) + 10, Integer), CType(tt, Integer)), _
.Size = New Size(40, 40), _
.Font = New System.Drawing.Font("Microsoft Sans Serif", _
16.0!, System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, _
CType(222, Byte)), _
.TextAlign = System.Drawing.ContentAlignment.MiddleCenter, _
.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle, _
.BackColor = Color.Azure}
If i Mod 10 = 9 Then
tt = tt + 50
End If
Me.Controls.Add(aNewlbl(i))
Next i
End Sub
Private Sub btnRandom_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
btnRandom.Enabled = False
btnNew.Enabled = False
nCounter = nCounter + 1
RandomNumber()
btnRandom.Enabled = True
btnNew.Enabled = True
End Sub
Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
'Reset
nCounter = 0
lblCounter.Text = ""
lblNumber.Text = "B I N G O"
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
If ctrl.Name <> "lblNumber" And ctrl.Name <> "lblCounter" Then
ctrl.BackColor = Color.Azure
ctrl.Enabled = True
End If
End If
Next
PlaySound("lET'S START BINGO")
End Sub
Private Sub RandomNumber()
Dim RandomClass As New Random()
Dim RandomNumber As Integer
If nCounter <= 50 Then
'Create a new Random class in VB.NET
Do While True
RandomNumber = RandomClass.Next(1, 51)
If Me.Controls("Label" & RandomNumber.ToString).Enabled = True Then
Exit Do
End If
Loop
Else
MessageBox.Show("Complete BINGO")
Return
End If
lblCounter.Text = nCounter.ToString
lblNumber.Text = RandomNumber.ToString
PlaySound("Number " & lblNumber.Text)
Me.Controls("Label" & RandomNumber.ToString).Enabled = False
Me.Controls("Label" & RandomNumber.ToString).BackColor = Color.Red
If nCounter >= 50 Then
MessageBox.Show("B I N G O Complete !!!!", "BINGO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub PlaySound(ByVal Word As String)
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.speak(Word)
End Sub
End Class
แล้วลอง Run โปรเจกต์ดู จะปรากรฎหน้าตาโปรแกรม คล้าย ๆ แบบนี้ ลองเล่นดู อิๆๆเผื่อเปิดโต๊ะแข่ง กับ Bingo ตามงานวัด
1 ความคิดเห็น:
มีอธบาย code ด้วยน่าจะดีนะครับ
แสดงความคิดเห็น