1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| Imports System.ComponentModel
Imports System.Globalization
<ToolboxBitmap(GetType(ComponentTableau), "control_array.bmp")>
<ProvideProperty("GridCol", GetType(Button))>
<ProvideProperty("GridRow", GetType(Button))>
<DefaultEvent("Click")>
Public Class ComponentTableau
Inherits Component
Implements IExtenderProvider
Public Event Click(ByVal IndexRow As Integer, indexCol As Integer)
Private Class IndexInfo
Public ClientControl As Button
Public Col As Integer
Public Row As Integer
' Initialize ArrayIndex to the default value.
Public Sub New(ByVal client_control As Button)
ClientControl = client_control
Col = -1
Row = -1
End Sub
' Return True if ArrayIndex is the default.
Public Function IsDefault() As Boolean
Return (Col = -1 And Row = -1)
End Function
End Class
' Information about our client controls.
Private m_IndexInfo As New Hashtable
' We can only extend buttons.
Public Function CanExtend(extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
Return TypeOf extendee Is Button
End Function
' Return this control's ArrayIndex.
<Category("Data"), DefaultValue("-1")>
Public Function GetGridCol(ByVal client_control As Button) As Integer
' Get the button's IndexInfo if it exists.
Return GetIndexInfo(client_control).Col
End Function
<Category("Data"), DefaultValue("-1")>
Public Function GetGridRow(ByVal client_control As Button) As Integer
' Get the button's IndexInfo if it exists.
Return GetIndexInfo(client_control).Row
End Function
' Set this control's ArrayIndex.
<Category("Data"), DefaultValue("-1")>
Public Sub SetGridCol(ByVal client_control As Button, ByVal indexCol As Integer)
' Get this control's info.
Dim index_info As IndexInfo = GetIndexInfo(client_control)
' Set the new minimum value.
index_info.Col = indexCol
' Add or remove the IndexInfo if necessary.
AddOrRemoveIfNecessary(client_control, index_info)
End Sub
<Category("Data"), DefaultValue("-1")>
Public Sub SetGridRow(ByVal client_control As Button, ByVal indexRow As Integer)
' Get this control's info.
Dim index_info As IndexInfo = GetIndexInfo(client_control)
' Set the new minimum value.
index_info.Row = indexRow
' Add or remove the IndexInfo if necessary.
AddOrRemoveIfNecessary(client_control, index_info)
End Sub ' Remove the control from the array.
<Category("Data")>
Public Sub RemoveGridCol(ByVal client_control As Button)
' Set the control's index to the default -1.
SetGridCol(client_control, -1)
End Sub
<Category("Data")>
Public Sub RemoveGridRow(ByVal client_control As Button)
' Set the control's index to the default -1.
SetGridRow(client_control, -1)
End Sub
' Return this control's IndexInfo.
Private Function GetIndexInfo(ByVal client_control As Button) As IndexInfo
' See if we have IndexInfo for this control.
If m_IndexInfo.Contains(client_control) Then
' We have IndexInfo for this control. Return it.
Return CType(m_IndexInfo(client_control), IndexInfo)
Else
' We do not have IndexInfo for this control.
' Return a new default IndexInfo.
Return New IndexInfo(client_control)
End If
End Function
' Add or remove this IndexInfo if necessary.
Private Sub AddOrRemoveIfNecessary(ByVal client_control As Button, ByVal index_info As IndexInfo)
' See if the IndexInfo should be present but is not,
' or should not be present but is.
If index_info.IsDefault <> Not m_IndexInfo.Contains(client_control) Then
If index_info.IsDefault Then
' The IndexInfo should not be present but is.
m_IndexInfo.Remove(client_control)
RemoveHandler client_control.Click, AddressOf Client_Click
Else
' The IndexInfo should be present but is not.
m_IndexInfo.Add(client_control, index_info)
AddHandler client_control.Click, AddressOf Client_Click
End If
End If
End Sub
' Handles client button Click events by raising our
' Click event, passing it the button's index.
Private Sub Client_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Get the client and its IndexInfo.
Dim client_button As Button = DirectCast(sender, Button)
Dim index_info As IndexInfo = GetIndexInfo(client_button)
RaiseEvent Click(index_info.Col, index_info.Row)
End Sub
' Return an Collection holding
' the controls in the array.
Public Function Controls() As Collection
' Build the collection.
Dim result As New Collection
For Each index_info As IndexInfo In m_IndexInfo.Values
result.Add(index_info.ClientControl)
Next index_info
' Return the collection.
Return result
End Function
End Class |