'Code Behind
Partial Class _Default
Inherits System.Web.UI.Page
'Private workFlag As Boolean = False
Private Property workFlag() As Boolean
Get
If (ViewState("_WorkFlag") = Nothing) Then
ViewState("_WorkFlag") = False
End If
Return CType(ViewState("_WorkFlag"), Boolean)
End Get
Set(ByVal value As Boolean)
ViewState("_WorkFlag") = value
End Set
End Property
Protected Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Page.IsPostBack) Then
Response.Write("[Page load] ")
If (Me.workFlag) Then
OnAddHanlder()
Else
OnDeleteHandler()
End If
End If
End Sub
Protected Sub AllButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click
Dim ClickButton As System.Web.UI.WebControls.Button = sender
Response.Write("{" + ClickButton.ID + "}")
End Sub
Protected Sub ButtonAttachEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAttachEvent.Click
Me.workFlag = True
End Sub
Protected Sub ButtonDetachEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonDetachEvent.Click
Me.workFlag = False
End Sub
Private Sub OnAddHanlder()
AddHandler Me.Button4.Click, AddressOf Me.AllButton_Click
AddHandler Me.Button5.Click, AddressOf Me.AllButton_Click
Response.Write("[ปุ่มที่ 4,5 เชื่อมต่อแล้ว] ")
End Sub
Private Sub OnDeleteHandler()
RemoveHandler Me.Button4.Click, AddressOf Me.AllButton_Click
RemoveHandler Me.Button5.Click, AddressOf Me.AllButton_Click
Response.Write("[ยกเลิกการเชื่อมต่อปุ่มที่ 4,5] ")
End Sub
End Class
สามารถทำ object ทุกชนิดเป็น array ได้เลยโดยใช้ List ช่วย
Code (VB.NET)
Imports System.Collections.Generic
Code (VB.NET)
Dim ArrayButton As List(Of Button) = New List(Of Button)()
For i As Integer = 1 To 4
Dim aButton As Button = New Button()
aButton.Text = "Button" & i.ToString()
ArrayButton.Add(aButton)
Next
จะอ้างอิงแบบ array ก็ได้ Code (VB.NET)
Dim txt1 As String = ArrayButton(0).Text
Dim txt2 As String = ArrayButton(1).Text
Dim txt3 As String = ArrayButton(2).Text
Dim txt4 As String = ArrayButton(3).Text
หรือจะอ้างอิงแบบนี้ก็ได้ Code (VB.NET)
For Each aButton As Button In ArrayButton
txt1 &= aButton.Text
Next
Protected Sub AllButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click
Dim ClickButton As System.Web.UI.WebControls.Button = sender
Response.Write("{" + ClickButton.ID + "}")
End Sub
WinApp นะคะ
New Project เปล่ามาแล้วที่ Form1 ไม่ต้องใส่ control อะไรทั้งนั้น
ก้อป code ไปใส่ค่ะ
Code (VB.NET)
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 = 5
Private AttachEventButton As Button = New Button()
Private DetachEventButton As Button = New Button()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 400
Me.Height = 400
OnCreateButton()
End Sub
Private Sub OnCreateButton()
For i As Integer = 0 To Me.MaxButtonNumber - 1
Dim ActiveButton As Button = New Button()
With ActiveButton
.Text = "Button " + i.ToString()
.Height = 30
.Width = 100
.Left = 10
.Top = 10 + (i * (ActiveButton.Height + 4))
.Name = "ปุ่มทดสอบที่ " + i.ToString()
End With
Me.Controls.Add(ActiveButton)
ButtonList.Add(ActiveButton)
Next
With AttachEventButton
.Text = "แปะ event"
.Height = 30
.Width = 100
.Left = 200
.Top = ButtonList(0).Top
.Visible = True
End With
Me.Controls.Add(AttachEventButton)
With DetachEventButton
.Text = "ยกเลิก event"
.Height = 30
.Width = 100
.Left = AttachEventButton.Left
.Top = AttachEventButton.Top
.Visible = False
End With
Me.Controls.Add(DetachEventButton)
AddHandler AttachEventButton.Click, AddressOf Me.AttachEventButton_Click
AddHandler DetachEventButton.Click, AddressOf Me.detachEventButton_Click
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 + "] แล้ว ", "ตรวจสอบการกดปุ่ม")
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.Click, AddressOf AllButton_Click
Next
MessageBox.Show("เชื่อมต่อ event แล้ว ", "Event attach")
AttachEventButton.Visible = False
DetachEventButton.Visible = 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.Click, AddressOf AllButton_Click
Next
MessageBox.Show(Me, "ยกเลิกการเชื่อมต่อ event", "Event detach" _
, MessageBoxButtons.OK, MessageBoxIcon.Information)
AttachEventButton.Visible = True
DetachEventButton.Visible = False
End Sub
End Class
Dim ArrayButton As New List(Of Button)
For i As Integer = 1 To 10
Dim aButton As New Button()
aButton.Text = "Button" & i.ToString()
aButton.Location = New Point(15, 15 + (i * 20))
AddHandler aButton.Click, AddressOf aButton_Click
Me.Controls.Add(aButton)
Next
Code (VB.NET)
Protected Sub aButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim aButton As Button = CType(sender, Button)
Dim txt As String = aButton.Text
End Sub
Private Dim ArrayButton As List(Of Button)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ArrayButton = New As List(Of Button)()
For i As Integer = 1 To 10
Dim aButton As New Button()
aButton.Text = "Button" & i.ToString()
aButton.Location = New Point(15, 15 + (i * 20))
AddHandler aButton.Click, AddressOf aButton_Click
Me.Controls.Add(aButton)
Next
End Sub
Code (VB.NET)
Protected Sub aButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim aButton As Button = CType(sender, Button)
Dim txt As String = FindIndex(aButton.Text).ToString()
End Sub
Code (VB.NET)
Protected Function FindIndex(ByVal ButtonName As String) As Integer
Dim Result As Integer = 0
For Each aButton As Button In ArrayButton
If aButton.Text = ButtonName Then
Exit For
End If
Result += 1
Next
Return Result
End Function