Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class Form1
Private ButtonList As List(Of Button) = New List(Of Button)()
Private Const MaxButtonNumber As Integer = 24 * 40
Private AttachEventButton As Button = New Button()
Private DetachEventButton As Button = New Button()
Private Calculate As Button = New Button()
Private BlackColorButton As Button = New Button()
Private WhiteColorButton As Button = New Button()
Private WhiteAll As Button = New Button()
Private BlackAll As Button = New Button()
Dim ArrayLED(MaxButtonNumber) As Integer
Dim j As Integer
Dim NumberButton As Integer
Dim BtLeft As Integer = 20
Dim bufferMCS51 As String
Dim PenColor As String = "White"
Dim testt As Integer = 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 1000
Me.Height = 600
OnCreateButton()
End Sub
Private Sub AllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ClickButton As Button = sender
'MessageBox.Show("กดปุ่ม [" + ClickButton.Name + "] แล้ว ", "ตรวจสอบการกดปุ่ม")
'//////////// convert name bt to array ///////////////////////////////////
NumberButton = CInt(ClickButton.Name)
If (PenColor = "White") Then
ClickButton.BackColor = Color.White
ArrayLED(NumberButton) = 0
Else
ClickButton.BackColor = Color.Black
ArrayLED(NumberButton) = 1
End If
End Sub
Private Sub AttachEventButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each itemButton As Button In ButtonList
AddHandler itemButton.MouseEnter, AddressOf AllButton_Click
Next
MessageBox.Show(Me, "เชื่อมต่อปุ่มแล้ว ", "Event attach")
AttachEventButton.Visible = False
DetachEventButton.Visible = True
Calculate.Enabled = True
End Sub
Private Sub detachEventButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each itemButton As Button In ButtonList
RemoveHandler itemButton.MouseEnter, AddressOf AllButton_Click
Next
MessageBox.Show(Me, "ยกเลิกการเชื่อมต่อปุ่ม", "Event detach" _
, MessageBoxButtons.OK, MessageBoxIcon.Information)
AttachEventButton.Visible = True
DetachEventButton.Visible = False
Calculate.Enabled = False
End Sub
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'//////////// Constance sennt to MCS-51 = SentMCS51(j) ////////////
Dim SentMCS51(MaxButtonNumber / 8) As String
Dim j As Integer = 0
For i = 0 To MaxButtonNumber - 1
bufferMCS51 = bufferMCS51 + ArrayLED(i).ToString
'/////////////////////// cal hex ///////////////////////////////
'MsgBox("ปุ่มที่ " + i.ToString + "มีค่าเท่ากับ" + ArrayLED(i).ToString)
If i Mod 8 = 7 Then
CalculateHeximal()
SentMCS51(j) = bufferMCS51
bufferMCS51 = ""
j = j + 1
End If
Next
End Sub
Private Sub OnCreateButton()
For i As Integer = 0 To MaxButtonNumber - 1
Dim ActiveButton As Button = New Button()
With ActiveButton
If (testt = 1) Then
.BackColor = Color.White
Else
.BackColor = Color.Black
End If
.FlatStyle = FlatStyle.Flat
ArrayLED(i) = 0
'//////////////////// SIZE //////////////////////////
.Height = 20
.Width = 20
'/////////////////// X,Y ////////////////////////////
If (i Mod 24 > 0) Then
.Left = BtLeft
Else
BtLeft = BtLeft + 20
.Left = BtLeft
End If
'////////////////////////////////////////////////////
.Top = 30 + ((i Mod 24) * (ActiveButton.Height + 0))
'///////////////// NAME /////////////////////////////
.Name = i.ToString()
End With
Me.Controls.Add(ActiveButton)
ButtonList.Add(ActiveButton)
Next
'////////////////////////////////////////////////////////////
With AttachEventButton
.Text = "Connect Button"
.Height = 30
.Width = 120
.Left = ButtonList(MaxButtonNumber - 1).Left + 30
.Top = ButtonList(0).Top
.Visible = True
End With
Me.Controls.Add(AttachEventButton)
'////////////////////////////////////////////////////////////
With DetachEventButton
.Text = "Disconnect Button"
.Height = 30
.Width = 120
.Left = AttachEventButton.Left
.Top = AttachEventButton.Top
.Visible = False
End With
Me.Controls.Add(DetachEventButton)
'////////////////////////////////////////////////////////////
With Calculate
.Text = "Cal LED"
.Height = 30
.Width = 120
.Left = AttachEventButton.Left
.Top = AttachEventButton.Top + 40
.Visible = True
.Enabled = False
End With
Me.Controls.Add(Calculate)
'///////////////////////// Add event ///////////////////////////
'////////////////////////////////////////////////////////////
With BlackColorButton
.Height = 60
.Width = 60
.Left = Calculate.Left
.Top = Calculate.Top + 40
.Visible = True
.Image = WindowsApplication1.My.Resources.fanta_emo107
End With
Me.Controls.Add(BlackColorButton)
'////////////////////////////////////////////////////////////
With WhiteColorButton
.Height = 60
.Width = 60
.Left = Calculate.Left
.Top = Calculate.Top + 40
.Visible = False
.Image = WindowsApplication1.My.Resources.eraser1
End With
Me.Controls.Add(WhiteColorButton)
'////////////////////////////////////////////////////////////
With WhiteAll
.Height = 30
.Width = 60
.Left = WhiteColorButton.Right + 10
.Top = Calculate.Top + 40
.Visible = True
.BackColor = Color.Black
End With
Me.Controls.Add(WhiteAll)
'////////////////////////////////////////////////////////////
With BlackAll
.Height = 30
.Width = 60
.Left = WhiteColorButton.Right + 10
.Top = WhiteAll.Bottom + 5
.Visible = True
.BackColor = Color.White
End With
Me.Controls.Add(BlackAll)
'///////////////////////// Add event ///////////////////////////
AddHandler AttachEventButton.Click, AddressOf Me.AttachEventButton_Click
AddHandler DetachEventButton.Click, AddressOf Me.detachEventButton_Click
AddHandler Calculate.Click, AddressOf Me.Calculate_Click
AddHandler WhiteColorButton.Click, AddressOf Me.WhiteColorButton_Click
AddHandler BlackColorButton.Click, AddressOf Me.BlackColorButton_Click
AddHandler BlackAll.Click, AddressOf Me.BlackAll_Click
AddHandler WhiteAll.Click, AddressOf Me.WhiteAll_Click
End Sub
Public Sub CalculateHeximal()
'///////////// Binary to Decimal to Heximal /////////////////////
Dim NumberDecimal As Integer
Dim NumberBinary As String = bufferMCS51
Dim BitWeight As Integer = 0
Dim NumberHeximal As String = ""
Dim SubHeximal1 As String = ""
Dim SubHeximal2 As String = ""
'///////////////SUB bufferMCS51 //////////////////////////////////
SubHeximal1 = NumberBinary.Substring(0, 4)
SubHeximal2 = NumberBinary.Substring(4)
'///////////////////////// Case 1 ////////////////////////////////
For i = 4 To 1 Step -1
If Mid$(SubHeximal1, i, 1) <> 0 Then
NumberDecimal = NumberDecimal + ((2 ^ BitWeight) * Mid$(SubHeximal1, i, 1))
' เพิ่มค่าเลขยกกำลังขึ้นอีก 1
End If
BitWeight = BitWeight + 1
Next
SubHeximal1 = Hex(NumberDecimal).ToString
'/////////////////// Clear Buffer ////////////////////////////////
NumberDecimal = 0
BitWeight = 0
'/////////////////////////Case 2 /////////////////////////////////
For i = 4 To 1 Step -1
If Mid$(SubHeximal2, i, 1) <> 0 Then
NumberDecimal = NumberDecimal + ((2 ^ BitWeight) * Mid$(SubHeximal2, i, 1))
' เพิ่มค่าเลขยกกำลังขึ้นอีก 1
End If
BitWeight = BitWeight + 1
Next
SubHeximal2 = Hex(NumberDecimal).ToString
NumberHeximal = SubHeximal1 + SubHeximal2
bufferMCS51 = NumberHeximal
'/////////////////////////////////////////////////////////////////
End Sub
Private Sub BlackColorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PenColor = "Black"
BlackColorButton.Visible = False
WhiteColorButton.Visible = True
End Sub
Private Sub WhiteColorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PenColor = "White"
BlackColorButton.Visible = True
WhiteColorButton.Visible = False
End Sub
Private Sub WhiteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub BlackAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class