Click here to Skip to main content
15,893,381 members
Articles / Multimedia / GDI+

Extending Menu Items With Extender Component

Rate me:
Please Sign up or sign in to vote.
3.58/5 (5 votes)
18 Oct 20073 min read 23.9K   149   16  
This article will help you to extend menu items with properties represented by any objects you like.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions