|
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization
Imports System.Drawing.Design
Imports System.Drawing
'TODO: add icon to toolboxbitmap so that it showed in designer.
<ProvideProperty("MyProperty", GetType(MenuItem)), ToolboxBitmap(GetType(CustomMenuExtender), "MenuExtenderIcon.ico")> _
Public Class CustomMenuExtender
Inherits System.ComponentModel.Component
Implements IExtenderProvider
Public Sub New()
_property = New Hashtable
End Sub
'''<summary>This function decide whether we should extend component or we should not. </summary>
Public Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
If (TypeOf extendee Is MenuItem And Not TypeOf extendee Is CustomMenuExtender) Then
Return True
Else
Return False
End If
End Function
Private _property As Hashtable
Public ReadOnly Property MenuItems() As System.Collections.ICollection
Get
Return _property.Keys
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Editor(GetType(CommandEditor), GetType(UITypeEditor)), EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Function GetMyProperty(ByVal mnu As MenuItem) As Command
Return GetPropertiesForMenuItem(mnu)
End Function
<EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Sub SetMyProperty(ByVal mnu As MenuItem, ByVal value As Object)
If (Not _property.Contains(mnu)) Then
_property.Add(mnu, value)
Else
_property.Item(mnu) = value
End If
End Sub
#Region "Runtime helpers"
'''<summary>
'''This function has to provide access to any command by command unique idetnitier.
'''Note that this name and the name of the component that represents command are DIFFERENT!!!
'''</summary>
Public Function ReceiveCommandByName(ByVal CommandName As String) As Command
For Each com As Command In _property.Values
If com.CommandName = CommandName Then
Return com
End If
Next
End Function
Public Function RecieveMenuByCommandName(ByVal CommandName As String) As MenuItem
'Iterating through hash table when found command with such a name get menuitem for it
With _property.GetEnumerator
.Reset()
While .MoveNext
If (CType(.Value, Command).CommandName = CommandName) Then
Return CType(.Key, MenuItem)
End If
End While
End With
'if no commands with such a name was found then return Nothing
Return Nothing
End Function
Public Function ReceiveMenuByMenuText(ByVal MenuText As String) As MenuItem
For Each mnu As MenuItem In _property.Keys
If mnu.Text = MenuText Then
Return mnu
End If
Next
'If no menu item was found return Nothing
Return Nothing
End Function
Public Function ReceiveCommandByMenu(ByVal MenuItemObj As MenuItem) As Command
End Function
#End Region
#Region "Private Helpers"
'''<summary>This function gets menu item and return it`s properties if they are exists.
'''Otherwise adds them to hashtable of menuitems properties.
'''</summary>
Private Function GetPropertiesForMenuItem(ByVal key As Object) As Command
Dim mnuProperties As Command
If (_property.ContainsKey(key)) Then
mnuProperties = CType(_property(key), Command)
_property.Item(key) = mnuProperties
Else
mnuProperties = New Command
_property.Add(key, mnuProperties)
End If
Return mnuProperties
End Function
#End Region
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.