Imports System.Reflection
'Codice scritto interamente da Totem
'Copyright (C) 2007
Public Class ControlsMatrix(Of C As {Control, New})
Inherits TableLayoutPanel
Public Delegate Sub InitializeControl(ByVal Column As Int32, ByVal Row As Int32, ByVal Ctrl As C)
Public Delegate Function FilteringMethod(ByVal Ctrl As C) As Boolean
Private _Events
As New Dictionary(Of
String, EventInfo
)
Public ReadOnly Property Control(ByVal Column As Int32, ByVal Row As Int32) As C
Get
If MyBase.GetControlFromPosition(Column, Row) IsNot Nothing Then
Return MyBase.GetControlFromPosition(Column, Row)
Else
Return Nothing
End If
End Get
End Property
Public Shadows Property ColumnCount() As Int32
Get
Return MyBase.ColumnCount
End Get
Set(ByVal Value As Int32)
If Value > 0 Then
MyBase.ColumnCount = Value
Else
Throw New IndexOutOfRangeException
End If
End Set
End Property
Public Shadows Property RowCount() As Int32
Get
Return MyBase.RowCount
End Get
Set(ByVal Value As Int32)
If Value > 0 Then
MyBase.RowCount = Value
Else
Throw New IndexOutOfRangeException
End If
End Set
End Property
Public ReadOnly Property [Event](ByVal Index As Int32) As String
Get
If Index >= 0 And Index < _Events.Count Then
Dim I As Int32 = 0
For Each Key As String In _Events.Keys
If I = Index Then
Return Key
End If
I += 1
Next
Else
Throw New IndexOutOfRangeException
End If
End Get
End Property
Private Sub CreateEventInfoCollection()
Dim ThisType As Type = GetType(C)
For Each EI As EventInfo In ThisType.GetEvents
_Events.Add(EI.Name, EI)
Next
End Sub
Public Sub New(ByVal Columns As Int32, ByVal Rows As Int32, ByVal InitializeRoutine As InitializeControl)
MyBase.ColumnCount = Columns
MyBase.RowCount = Rows
Dim Ctrl As C
For Col As Int32 = 0 To Columns - 1
For Row As Int32 = 0 To Rows - 1
Ctrl = New C
InitializeRoutine(Col, Row, Ctrl)
MyBase.Controls.Add(Ctrl, Col, Row)
Next
Next
CreateEventInfoCollection()
End Sub
Public Sub New(ByVal Columns As Int32, ByVal Rows As Int32)
MyBase.ColumnCount = Columns
MyBase.RowCount = Rows
For Col As Int32 = 0 To Columns - 1
For Row As Int32 = 0 To Rows - 1
MyBase.Controls.Add(New C, Col, Row)
Next
Next
CreateEventInfoCollection()
End Sub
Public Sub New()
Me.New(1, 1)
CreateEventInfoCollection()
End Sub
Public Sub ResetBounds()
Dim TotalWidth, TotalHeight As Int32
Dim Temp As C
For Col As Int32 = 0 To Me.ColumnCount - 1
For Row As Int32 = 0 To Me.RowCount - 1
Temp = Me.Control(Col, Row)
TotalWidth += Temp.Width
TotalHeight += Temp.Height
Next
Next
TotalWidth += Me.ColumnCount * Me.Padding.Horizontal * 2
TotalHeight += Me.RowCount * Me.Padding.Vertical * 2
Me.Width = TotalWidth
Me.Height = TotalHeight
End Sub
Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate)
Try
Dim EventToAdd As EventInfo = _Events(EventName)
If EventToAdd IsNot Nothing Then
For Col As Int32 = 0 To Me.ColumnCount - 1
For Row As Int32 = 0 To Me.RowCount - 1
EventToAdd.AddEventHandler(Me.Control(Col, Row), Deleg)
Next
Next
End If
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal Column As Int32, ByVal Row As Int32)
Try
Dim EventToAdd As EventInfo = _Events(EventName)
Dim Ctrl As C = Me.Control(Column, Row)
If (EventToAdd IsNot Nothing) And (Ctrl IsNot Nothing) Then
EventToAdd.AddEventHandler(Ctrl, Deleg)
End If
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal FilterRoutine As FilteringMethod)
Try
Dim EventToAdd As EventInfo = _Events(EventName)
If EventToAdd Is Nothing Then
Exit Sub
End If
Dim Ctrls As New List(Of C)
Dim Ctrl As C
For Col As Int32 = 0 To Me.ColumnCount - 1
For Row As Int32 = 0 To Me.RowCount - 1
Ctrl = Me.Control(Col, Row)
If FilterRoutine(Ctrl) Then
Ctrls.Add(Ctrl)
End If
Next
Next
For Each Ctrl In Ctrls
EventToAdd.AddEventHandler(Ctrl, Deleg)
Next
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate)
Try
Dim EventToDel As EventInfo = _Events(EventName)
If EventToDel IsNot Nothing Then
For Col As Int32 = 0 To Me.ColumnCount - 1
For Row As Int32 = 0 To Me.RowCount - 1
EventToDel.RemoveEventHandler(Me.Control(Col, Row), Deleg)
Next
Next
End If
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal Column As Int32, ByVal Row As Int32)
Try
Dim EventToDel As EventInfo = _Events(EventName)
Dim Ctrl As C = Me.Control(Column, Row)
If (EventToDel IsNot Nothing) And (Ctrl IsNot Nothing) Then
EventToDel.RemoveEventHandler(Ctrl, Deleg)
End If
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal FilterRoutine As FilteringMethod)
Try
Dim EventToDel As EventInfo = _Events(EventName)
If EventToDel Is Nothing Then
Exit Sub
End If
Dim Ctrls As New List(Of C)
Dim Ctrl As C
For Col As Int32 = 0 To Me.ColumnCount - 1
For Row As Int32 = 0 To Me.RowCount - 1
Ctrl = Me.Control(Col, Row)
If FilterRoutine(Ctrl) Then
Ctrls.Add(Ctrl)
End If
Next
Next
For Each Ctrl In Ctrls
EventToDel.RemoveEventHandler(Ctrl, Deleg)
Next
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub
Public Function GetEvents() As String()
Dim Temp(_Events.Count - 1) As String
Dim Index As Int32 = 0
For Each EventName As String In _Events.Keys
Temp(Index) = EventName
Index += 1
Next
Return Temp
End Function
Public Overloads Sub AddControls(ByVal Count As Int32)
For I As Int32 = 0 To Count - 1
Me.Controls.Add(New C)
Next
End Sub
Public Overloads Sub AddControls(ByVal Count As Int32, ByVal InitializeRoutine As InitializeControl)
Dim Ctrl As C
For I As Int32 = 0 To Count - 1
Ctrl = New C
InitializeRoutine(Me.Controls.Count Mod Me.ColumnCount - 1, Me.Controls.Count Mod Me.RowCount - 1, Ctrl)
Me.Controls.Add(Ctrl)
Next
End Sub
Public Overloads Sub RemoveControls(ByVal StartColumn As Int32, ByVal StartRow As Int32, Optional ByVal IncludeThis As Boolean = True)
If StartColumn >= 0 And StartColumn < Me.ColumnCount And _
StartRow >= 0 And StartRow < Me.RowCount Then
Dim ControlsToDel As New List(Of C)
For Row As Int32 = StartRow To Me.RowCount - 1
If Row = StartRow Then
For Col As Int32 = StartColumn To Me.ColumnCount - 1
ControlsToDel.Add(Me.Control(Col, Row))
Next
Else
For Col As Int32 = 0 To Me.ColumnCount - 1
ControlsToDel.Add(Me.Control(Col, Row))
Next
End If
Next
If Not IncludeThis Then
ControlsToDel.Remove(Me.Control(StartColumn, StartRow))
End If
For Each Ctrl As C In ControlsToDel
Me.Controls.Remove(Ctrl)
Next
End If
End Sub
Public Overloads Sub RemoveControls(ByVal StartColumn As Int32, ByVal StartRow As Int32, ByVal EndColumn As Int32, ByVal EndRow As Int32)
If (StartColumn >= 0 And StartColumn < Me.ColumnCount) AndAlso _
(StartRow >= 0 And StartRow < Me.RowCount) AndAlso _
(EndColumn >= 0 And EndColumn < Me.ColumnCount) AndAlso _
(EndRow >= 0 And EndRow < Me.RowCount) AndAlso _
((EndRow > StartRow) Or (EndRow = StartRow And (EndColumn > StartColumn))) Then
Dim ControlsToDel As New List(Of C)
For Row As Int32 = StartRow To EndRow
If Row = StartRow Then
Dim ToCol As Int32 = Me.ColumnCount - 1
If StartRow = EndRow Then
ToCol = EndColumn
End If
For Col As Int32 = StartColumn To ToCol
ControlsToDel.Add(Me.Control(Col, Row))
Next
ElseIf Row = EndRow Then
For Col As Int32 = 0 To EndColumn
ControlsToDel.Add(Me.Control(Col, Row))
Next
Else
For Col As Int32 = 0 To Me.ColumnCount - 1
ControlsToDel.Add(Me.Control(Col, Row))
Next
End If
Next
For Each Ctrl As C In ControlsToDel
Me.Controls.Remove(Ctrl)
Next
End If
End Sub
End Class