![]() |
Desktop Development »
Menus »
Toolbars
Intermediate
Extended Interface for ToolbarsBy psdavisThis extension allows toolbar buttons to call functions automatically |
C#, Windows, .NET 1.0, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
ToolbarButtons in .NET lack the basic capability of performing a task on their
own. It would be very convenient to set up all toolbar events
directly inside of the toolbar itself.
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 ToolBarButtonClickEventArgs
parameter into a MenuItem.
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.
The IExtenderProvider is a tremendously powerful tool when used in
conjunction with the interface, yet the syntax is quite simple. Basically
you need to create a Get/Set pairing similar to a standard C# property, but
with the full name displayed.
The ProvideProperty tag tells the interface that you are adding a property
called "ToolbarFunction" to all of your components in your current
development. The CanExtend function narrows the
components that receive this property to only ToolBarButtons.
Each ToolBarButton will receive a new property that will allow each button to
be assigned to a specific MenuItem.
[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.
After copying the Extended Interface DLL (ExtendedInterface.DLL) to your desired destination, you will need to add it permanently to your toolbox.
Browse...
The Toolbox should have two new components: StatusMessage and ToolBarFunction.
The other component StatusMessage was discussed in an
earlier article.

An optional step is to use meaningful names for the (Name) parameter of each
MenuItem. Names like MenuItem23 are certainly usable, but when you have to
manage functionality based on those names, it can get quite complicated.
Now it's time to add the ToolBarFunction to your application. Drag one over from
your Toolbox and it should appear below the form with the other components.
You will, of course, need to have a ToolBar on your form. Drag one over from
your Toolbox if you haven't already.
ToolBar.
ToolbarFunction' property that the component has added for you.
Use the drop down list to view all of your menu items.
MenuItem that you want the button to use when clicked.

Once you have a ToolBarButton tied into a MenuItem, clicking on a button will
have the same effect as clicking the menu! That was just too easy.
[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( );
}
}
}
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Apr 2002 Editor: Chris Maunder |
Copyright 2002 by psdavis Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |