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 * 120
'Private Const MaxButtonNumber As Integer = 8 * 8
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()
Private ChangeColor As Button = New Button()
Private PaintMode As ComboBox = New ComboBox()
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
Dim Mode As String = "Point Mode"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 1300
Me.Height = 750
OnCreateButton()
OnCreateCombobox()
End Sub
Private Sub AllButton_Move(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim MoveButton As Button = sender
'MessageBox.Show("กดปุ่ม [" + ClickButton.Name + "] แล้ว ", "ตรวจสอบการกดปุ่ม")
'//////////// convert name bt to array ///////////////////////////////////
NumberButton = CInt(MoveButton.Name)
If (Mode = "Line Mode") Then
If (PenColor = "White") Then
MoveButton.BackColor = Color.White
'MoveButton.Image = WindowsApplication1.My.Resources.white
ArrayLED(NumberButton) = 0
Else
MoveButton.BackColor = Color.Black
'MoveButton.Image = WindowsApplication1.My.Resources.black
ArrayLED(NumberButton) = 1
End If
End If
End Sub
Private Sub AllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ClickButton As Button = sender
Dim i As Integer = 0
'MessageBox.Show("กดปุ่ม [" + ClickButton.Name + "] แล้ว ", "ตรวจสอบการกดปุ่ม")
'//////////// convert name bt to array ///////////////////////////////////
NumberButton = CInt(ClickButton.Name)
If (Mode = "Point Mode") Then
If (ArrayLED(CInt(ClickButton.Name)) = 1 = 0) Then
ClickButton.BackColor = Color.Black
'ClickButton.Image = WindowsApplication1.My.Resources.black
ArrayLED(NumberButton) = 1
Else
ClickButton.BackColor = Color.White
'ClickButton.Image = WindowsApplication1.My.Resources.white
ArrayLED(NumberButton) = 0
End If
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_Move
AddHandler itemButton.Click, AddressOf AllButton_Click
Next
MessageBox.Show(Me, "เชื่อมต่อปุ่มแล้ว ", "Event attach")
AttachEventButton.Visible = False
DetachEventButton.Visible = True
Calculate.Enabled = True
BlackColorButton.Enabled = True
WhiteColorButton.Enabled = True
BlackAll.Enabled = True
WhiteAll.Enabled = True
ChangeColor.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_Move
RemoveHandler itemButton.Click, AddressOf AllButton_Click
Next
MessageBox.Show(Me, "ยกเลิกการเชื่อมต่อปุ่ม", "Event detach" _
, MessageBoxButtons.OK, MessageBoxIcon.Information)
AttachEventButton.Visible = True
DetachEventButton.Visible = False
Calculate.Enabled = False
BlackColorButton.Enabled = False
WhiteColorButton.Enabled = False
BlackAll.Enabled = False
WhiteAll.Enabled = False
ChangeColor.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 test 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
test = (test & SentMCS51(j) & vbCrLf)
'MsgBox(bufferMCS51.ToString + (j + 1).ToString)
bufferMCS51 = ""
j = j + 1
End If
Next
MsgBox(test)
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 = 27
.Width = 27
'/////////////////// X,Y ////////////////////////////
If (i Mod 24 > 0) Then
.Left = BtLeft
Else
BtLeft = BtLeft + 27
.Left = BtLeft
End If
'////////////////////////////////////////////////////
.Top = 30 + ((i Mod 24) * (ActiveButton.Height + 0))
'///////////////// NAME /////////////////////////////
.Name = i.ToString()
.BackColor = Color.White
' .Image = WindowsApplication1.My.Resources.white
.BackColor = Color.White
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
.BackColor = Color.Black
'.Image = WindowsApplication1.My.Resources.fanta_emo107
.Enabled = False
End With
Me.Controls.Add(BlackColorButton)
'////////////////////////////////////////////////////////////
With WhiteColorButton
.Height = 60
.Width = 60
.Left = Calculate.Left
.Top = Calculate.Top + 40
.Visible = False
.BackColor = Color.White
' .Image = WindowsApplication1.My.Resources.eraser1
.Enabled = False
End With
Me.Controls.Add(WhiteColorButton)
'////////////////////////////////////////////////////////////
With WhiteAll
.Height = 20
.Width = 50
.Left = WhiteColorButton.Right + 10
.Top = Calculate.Top + 40
.Visible = True
.BackColor = Color.White
.Enabled = False
End With
Me.Controls.Add(WhiteAll)
'////////////////////////////////////////////////////////////
With BlackAll
.Height = 20
.Width = 50
.Left = WhiteColorButton.Right + 10
.Top = WhiteAll.Bottom + 2
.Visible = True
.BackColor = Color.Black
.Enabled = False
End With
Me.Controls.Add(BlackAll)
'////////////////////////////////////////////////////////////
With ChangeColor
.Height = 20
.Width = 50
.Left = WhiteColorButton.Right + 10
.Top = BlackAll.Bottom + 2
.Visible = True
'.Image = WindowsApplication1.My.Resources.wbbw
.Enabled = False
End With
Me.Controls.Add(ChangeColor)
'///////////////////////// 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
AddHandler ChangeColor.Click, AddressOf Me.ChangeColor_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)
For Each itemButton As Button In ButtonList
itemButton.BackColor = Color.White
' itemButton.Image = WindowsApplication1.My.Resources.white
ArrayLED(CInt(itemButton.Name)) = 0
Next
End Sub
Private Sub BlackAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each itemButton As Button In ButtonList
itemButton.BackColor = Color.Black
'itemButton.Image = WindowsApplication1.My.Resources.black
ArrayLED(CInt(itemButton.Name)) = 1
Next
End Sub
Private Sub ChangeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each itemButton As Button In ButtonList
If (ArrayLED(CInt(itemButton.Name)) = 1) Then
itemButton.BackColor = Color.White
'itemButton.Image = WindowsApplication1.My.Resources.white
ArrayLED(CInt(itemButton.Name)) = 0
Else
itemButton.BackColor = Color.Black
'itemButton.Image = WindowsApplication1.My.Resources.black
ArrayLED(CInt(itemButton.Name)) = 1
End If
Next
End Sub
Public Sub OnCreateCombobox()
With PaintMode
.Left = BlackColorButton.Left
.Top = BlackAll.Bottom + 30
.Text = ("---Select Mode---")
.Items.Add("Point Mode")
.Items.Add("Line Mode")
'.Enabled = False
End With
Me.Controls.Add(PaintMode)
AddHandler PaintMode.SelectedIndexChanged, AddressOf Me.PaintMode_SelectIndexChange
End Sub
Private Sub PaintMode_SelectIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (PaintMode.Text = "Line Mode") Then
Mode = "Line Mode"
ElseIf (PaintMode.Text = "Point Mode") Then
Mode = "Point Mode"
End If
End Sub
End Class