Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Control Matrix - ControlsMatrix.vb

ControlsMatrix.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Reflection
  2. 'Codice scritto interamente da Totem
  3. 'Copyright (C) 2007
  4. Public Class ControlsMatrix(Of C As {Control, New})
  5.     Inherits TableLayoutPanel
  6.  
  7.     Public Delegate Sub InitializeControl(ByVal Column As Int32, ByVal Row As Int32, ByVal Ctrl As C)
  8.     Public Delegate Function FilteringMethod(ByVal Ctrl As C) As Boolean
  9.  
  10.     Private _Events As New Dictionary(Of String, EventInfo)
  11.  
  12.     Public ReadOnly Property Control(ByVal Column As Int32, ByVal Row As Int32) As C
  13.         Get
  14.             If MyBase.GetControlFromPosition(Column, Row) IsNot Nothing Then
  15.                 Return MyBase.GetControlFromPosition(Column, Row)
  16.             Else
  17.                 Return Nothing
  18.             End If
  19.         End Get
  20.     End Property
  21.  
  22.     Public Shadows Property ColumnCount() As Int32
  23.         Get
  24.             Return MyBase.ColumnCount
  25.         End Get
  26.         Set(ByVal Value As Int32)
  27.             If Value > 0 Then
  28.                 MyBase.ColumnCount = Value
  29.             Else
  30.                 Throw New IndexOutOfRangeException
  31.             End If
  32.         End Set
  33.     End Property
  34.  
  35.     Public Shadows Property RowCount() As Int32
  36.         Get
  37.             Return MyBase.RowCount
  38.         End Get
  39.         Set(ByVal Value As Int32)
  40.             If Value > 0 Then
  41.                 MyBase.RowCount = Value
  42.             Else
  43.                 Throw New IndexOutOfRangeException
  44.             End If
  45.         End Set
  46.     End Property
  47.  
  48.     Public ReadOnly Property [Event](ByVal Index As Int32) As String
  49.         Get
  50.             If Index >= 0 And Index < _Events.Count Then
  51.                 Dim I As Int32 = 0
  52.                 For Each Key As String In _Events.Keys
  53.                     If I = Index Then
  54.                         Return Key
  55.                     End If
  56.                     I += 1
  57.                 Next
  58.             Else
  59.                 Throw New IndexOutOfRangeException
  60.             End If
  61.         End Get
  62.     End Property
  63.  
  64.     Private Sub CreateEventInfoCollection()
  65.         Dim ThisType As Type = GetType(C)
  66.  
  67.         For Each EI As EventInfo In ThisType.GetEvents
  68.             _Events.Add(EI.Name, EI)
  69.         Next
  70.     End Sub
  71.  
  72.     Public Sub New(ByVal Columns As Int32, ByVal Rows As Int32, ByVal InitializeRoutine As InitializeControl)
  73.         MyBase.ColumnCount = Columns
  74.         MyBase.RowCount = Rows
  75.  
  76.         Dim Ctrl As C
  77.  
  78.         For Col As Int32 = 0 To Columns - 1
  79.             For Row As Int32 = 0 To Rows - 1
  80.                 Ctrl = New C
  81.                 InitializeRoutine(Col, Row, Ctrl)
  82.                 MyBase.Controls.Add(Ctrl, Col, Row)
  83.             Next
  84.         Next
  85.  
  86.         CreateEventInfoCollection()
  87.     End Sub
  88.  
  89.     Public Sub New(ByVal Columns As Int32, ByVal Rows As Int32)
  90.         MyBase.ColumnCount = Columns
  91.         MyBase.RowCount = Rows
  92.  
  93.         For Col As Int32 = 0 To Columns - 1
  94.             For Row As Int32 = 0 To Rows - 1
  95.                 MyBase.Controls.Add(New C, Col, Row)
  96.             Next
  97.         Next
  98.  
  99.         CreateEventInfoCollection()
  100.     End Sub
  101.  
  102.     Public Sub New()
  103.         Me.New(1, 1)
  104.         CreateEventInfoCollection()
  105.     End Sub
  106.  
  107.     Public Sub ResetBounds()
  108.         Dim TotalWidth, TotalHeight As Int32
  109.         Dim Temp As C
  110.  
  111.         For Col As Int32 = 0 To Me.ColumnCount - 1
  112.             For Row As Int32 = 0 To Me.RowCount - 1
  113.                 Temp = Me.Control(Col, Row)
  114.                 TotalWidth += Temp.Width
  115.                 TotalHeight += Temp.Height
  116.             Next
  117.         Next
  118.         TotalWidth += Me.ColumnCount * Me.Padding.Horizontal * 2
  119.         TotalHeight += Me.RowCount * Me.Padding.Vertical * 2
  120.  
  121.         Me.Width = TotalWidth
  122.         Me.Height = TotalHeight
  123.     End Sub
  124.  
  125.     Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate)
  126.         Try
  127.             Dim EventToAdd As EventInfo = _Events(EventName)
  128.  
  129.             If EventToAdd IsNot Nothing Then
  130.                 For Col As Int32 = 0 To Me.ColumnCount - 1
  131.                     For Row As Int32 = 0 To Me.RowCount - 1
  132.                         EventToAdd.AddEventHandler(Me.Control(Col, Row), Deleg)
  133.                     Next
  134.                 Next
  135.             End If
  136.         Catch Ex As Exception
  137.             Throw New Exception(Ex.Message)
  138.         End Try
  139.     End Sub
  140.  
  141.     Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal Column As Int32, ByVal Row As Int32)
  142.         Try
  143.             Dim EventToAdd As EventInfo = _Events(EventName)
  144.             Dim Ctrl As C = Me.Control(Column, Row)
  145.  
  146.             If (EventToAdd IsNot Nothing) And (Ctrl IsNot Nothing) Then
  147.                 EventToAdd.AddEventHandler(Ctrl, Deleg)
  148.             End If
  149.         Catch Ex As Exception
  150.             Throw New Exception(Ex.Message)
  151.         End Try
  152.     End Sub
  153.  
  154.     Public Overloads Sub [AddHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal FilterRoutine As FilteringMethod)
  155.         Try
  156.             Dim EventToAdd As EventInfo = _Events(EventName)
  157.  
  158.             If EventToAdd Is Nothing Then
  159.                 Exit Sub
  160.             End If
  161.  
  162.             Dim Ctrls As New List(Of C)
  163.             Dim Ctrl As C
  164.  
  165.             For Col As Int32 = 0 To Me.ColumnCount - 1
  166.                 For Row As Int32 = 0 To Me.RowCount - 1
  167.                     Ctrl = Me.Control(Col, Row)
  168.                     If FilterRoutine(Ctrl) Then
  169.                         Ctrls.Add(Ctrl)
  170.                     End If
  171.                 Next
  172.             Next
  173.  
  174.             For Each Ctrl In Ctrls
  175.                 EventToAdd.AddEventHandler(Ctrl, Deleg)
  176.             Next
  177.         Catch Ex As Exception
  178.             Throw New Exception(Ex.Message)
  179.         End Try
  180.     End Sub
  181.  
  182.     Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate)
  183.         Try
  184.             Dim EventToDel As EventInfo = _Events(EventName)
  185.  
  186.             If EventToDel IsNot Nothing Then
  187.                 For Col As Int32 = 0 To Me.ColumnCount - 1
  188.                     For Row As Int32 = 0 To Me.RowCount - 1
  189.                         EventToDel.RemoveEventHandler(Me.Control(Col, Row), Deleg)
  190.                     Next
  191.                 Next
  192.             End If
  193.         Catch Ex As Exception
  194.             Throw New Exception(Ex.Message)
  195.         End Try
  196.     End Sub
  197.  
  198.     Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal Column As Int32, ByVal Row As Int32)
  199.         Try
  200.             Dim EventToDel As EventInfo = _Events(EventName)
  201.             Dim Ctrl As C = Me.Control(Column, Row)
  202.  
  203.             If (EventToDel IsNot Nothing) And (Ctrl IsNot Nothing) Then
  204.                 EventToDel.RemoveEventHandler(Ctrl, Deleg)
  205.             End If
  206.         Catch Ex As Exception
  207.             Throw New Exception(Ex.Message)
  208.         End Try
  209.     End Sub
  210.  
  211.     Public Overloads Sub [RemoveHandler](ByVal EventName As String, ByVal Deleg As System.Delegate, ByVal FilterRoutine As FilteringMethod)
  212.         Try
  213.             Dim EventToDel As EventInfo = _Events(EventName)
  214.  
  215.             If EventToDel Is Nothing Then
  216.                 Exit Sub
  217.             End If
  218.  
  219.             Dim Ctrls As New List(Of C)
  220.             Dim Ctrl As C
  221.  
  222.             For Col As Int32 = 0 To Me.ColumnCount - 1
  223.                 For Row As Int32 = 0 To Me.RowCount - 1
  224.                     Ctrl = Me.Control(Col, Row)
  225.                     If FilterRoutine(Ctrl) Then
  226.                         Ctrls.Add(Ctrl)
  227.                     End If
  228.                 Next
  229.             Next
  230.  
  231.             For Each Ctrl In Ctrls
  232.                 EventToDel.RemoveEventHandler(Ctrl, Deleg)
  233.             Next
  234.         Catch Ex As Exception
  235.             Throw New Exception(Ex.Message)
  236.         End Try
  237.     End Sub
  238.  
  239.     Public Function GetEvents() As String()
  240.         Dim Temp(_Events.Count - 1) As String
  241.         Dim Index As Int32 = 0
  242.  
  243.         For Each EventName As String In _Events.Keys
  244.             Temp(Index) = EventName
  245.             Index += 1
  246.         Next
  247.  
  248.         Return Temp
  249.     End Function
  250.  
  251.     Public Overloads Sub AddControls(ByVal Count As Int32)
  252.         For I As Int32 = 0 To Count - 1
  253.             Me.Controls.Add(New C)
  254.         Next
  255.     End Sub
  256.  
  257.     Public Overloads Sub AddControls(ByVal Count As Int32, ByVal InitializeRoutine As InitializeControl)
  258.         Dim Ctrl As C
  259.         For I As Int32 = 0 To Count - 1
  260.             Ctrl = New C
  261.             InitializeRoutine(Me.Controls.Count Mod Me.ColumnCount - 1, Me.Controls.Count Mod Me.RowCount - 1, Ctrl)
  262.             Me.Controls.Add(Ctrl)
  263.         Next
  264.     End Sub
  265.  
  266.     Public Overloads Sub RemoveControls(ByVal StartColumn As Int32, ByVal StartRow As Int32, Optional ByVal IncludeThis As Boolean = True)
  267.         If StartColumn >= 0 And StartColumn < Me.ColumnCount And _
  268.            StartRow >= 0 And StartRow < Me.RowCount Then
  269.             Dim ControlsToDel As New List(Of C)
  270.  
  271.             For Row As Int32 = StartRow To Me.RowCount - 1
  272.                 If Row = StartRow Then
  273.                     For Col As Int32 = StartColumn To Me.ColumnCount - 1
  274.                         ControlsToDel.Add(Me.Control(Col, Row))
  275.                     Next
  276.                 Else
  277.                     For Col As Int32 = 0 To Me.ColumnCount - 1
  278.                         ControlsToDel.Add(Me.Control(Col, Row))
  279.                     Next
  280.                 End If
  281.             Next
  282.  
  283.             If Not IncludeThis Then
  284.                 ControlsToDel.Remove(Me.Control(StartColumn, StartRow))
  285.             End If
  286.  
  287.             For Each Ctrl As C In ControlsToDel
  288.                 Me.Controls.Remove(Ctrl)
  289.             Next
  290.         End If
  291.     End Sub
  292.  
  293.     Public Overloads Sub RemoveControls(ByVal StartColumn As Int32, ByVal StartRow As Int32, ByVal EndColumn As Int32, ByVal EndRow As Int32)
  294.         If (StartColumn >= 0 And StartColumn < Me.ColumnCount) AndAlso _
  295.            (StartRow >= 0 And StartRow < Me.RowCount) AndAlso _
  296.            (EndColumn >= 0 And EndColumn < Me.ColumnCount) AndAlso _
  297.            (EndRow >= 0 And EndRow < Me.RowCount) AndAlso _
  298.            ((EndRow > StartRow) Or (EndRow = StartRow And (EndColumn > StartColumn))) Then
  299.  
  300.             Dim ControlsToDel As New List(Of C)
  301.  
  302.             For Row As Int32 = StartRow To EndRow
  303.                 If Row = StartRow Then
  304.                     Dim ToCol As Int32 = Me.ColumnCount - 1
  305.                     If StartRow = EndRow Then
  306.                         ToCol = EndColumn
  307.                     End If
  308.                     For Col As Int32 = StartColumn To ToCol
  309.                         ControlsToDel.Add(Me.Control(Col, Row))
  310.                     Next
  311.                 ElseIf Row = EndRow Then
  312.                     For Col As Int32 = 0 To EndColumn
  313.                         ControlsToDel.Add(Me.Control(Col, Row))
  314.                     Next
  315.                 Else
  316.                     For Col As Int32 = 0 To Me.ColumnCount - 1
  317.                         ControlsToDel.Add(Me.Control(Col, Row))
  318.                     Next
  319.                 End If
  320.             Next
  321.  
  322.             For Each Ctrl As C In ControlsToDel
  323.                 Me.Controls.Remove(Ctrl)
  324.             Next
  325.         End If
  326.     End Sub
  327. End Class