|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Problem
The current accepted practice is to override the ButtonClick event and use a case statement to decide what button was pressed and what function should be called. The scribble example given by Microsoft is displayed below. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { if( e.Button == newButton ) { New( ); } else if( e.Button == openButton ) { Open( ); } else if( e.Button == saveButton ) { Save( ); } else if( e.Button == previewButton ) { PrintPreview( ); } else if( e.Button == printButton ) { Print( ); } else if( e.Button == helpButton ) { ShowHelpTopics(); } } Or you could devise some sort of method for looking at the string text. I've seen one example that was similar to this. void ToolBarOnClick( object obj, ToolBarButtonClickEventArgs e ) { switch( e.Button.Text ) { case "&Open": ... break; case "&Close": ... break; } } Another process described by Petzolt will convert the void ToolBarOnClick( object obj, ToolBarButtonClickEventArgs e ) { ToolBarButton btn = e.Button; MenuItem mnu = (MenuItem) btn.Tag; mnu.PerformClick( ); } Hopefully, we can overcome these shortcoming with a quick and simple extended property. Plus, give you a sample to create your own extended interfaces. BackgroundThe The ProvideProperty tag tells the interface that you are adding a property
called [ProvideProperty( "ToolbarFunction", typeof(Component)) ]
public class ToolbarFunction : Component, IExtenderProvider
{
public void SetToolbarFunction( Component pComponent,
MenuItem pMenuItem )
...
public MenuItem GetToolbarFunction( Component pComponent )
...
public bool CanExtend( object pComponent )
{
return( pComponent is ToolBarButton );
}
}
The source code for the interface is quite simple, but I'll refrain from discussing the actual coding process to keep this article on a basic level. 1. Add the ToolBarFunction component to .NETAfter copying the Extended Interface DLL (ExtendedInterface.DLL) to your desired destination, you will need to add it permanently to your toolbox.
The Toolbox should have two new components: The other component 2. Optional: Give useful names to your menus
An optional step is to use meaningful names for the (Name) parameter of each
3. Add the ToolBarFunction component to your form.Now it's time to add the 4. Assign MenuItem functionality to your toolbar.You will, of course, need to have a
5. Test your applicationOnce you have a Source Code
[ProvideProperty( "ToolbarFunction", typeof(Component)) ]
public class ToolbarFunction : Component, IExtenderProvider
{
ToolBar m_ToolBar = null;
Hashtable m_Dictionary = new Hashtable( );
public void SetToolbarFunction( Component pComponent, MenuItem pMenuItem )
{
if( ! m_Dictionary.Contains( pComponent ))
{
m_Dictionary.Add( pComponent, pMenuItem );
if( m_ToolBar == null )
{
ToolBarButton pToolBarButton = pComponent as ToolBarButton;
if( pToolBarButton != null )
{
m_ToolBar = pToolBarButton.Parent as ToolBar;
if( m_ToolBar != null )
m_ToolBar.ButtonClick +=
new ToolBarButtonClickEventHandler( Handle_ToolbarButtonClick );
}
}
}
else
{
m_Dictionary[ pComponent ] = pMenuItem;
}
}
public MenuItem GetToolbarFunction( Component pComponent )
{
if( m_Dictionary.Contains( pComponent ))
return (MenuItem) m_Dictionary[ pComponent ];
return null;
}
public bool CanExtend( object pComponent )
{
return( pComponent is ToolBarButton );
}
private void Handle_ToolbarButtonClick( object pComponent,
ToolBarButtonClickEventArgs e )
{
if( m_Dictionary.Contains( e.Button ))
{
MenuItem pMenuItem = (MenuItem) m_Dictionary[ e.Button ];
if( pMenuItem != null )
pMenuItem.PerformClick( );
}
}
}
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||