Click here to Skip to main content
13,589,335 members
Click here to Skip to main content
Add your own
alternative version


86 bookmarked
Posted 10 Apr 2002

Extended Interface for Toolbars

Rate this:
Please Sign up or sign in to vote.
This extension allows toolbar buttons to call functions automatically


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":
      case "&Close":





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.

1. Add the ToolBarFunction component to .NET

After copying the Extended Interface DLL (ExtendedInterface.DLL) to your desired destination, you will need to add it permanently to your toolbox. 

  1. With the toolbox open, right click the list to select 'Customize Toolbox.
  2. Select the tab .NET Framework Components
  3. Click Browse...
  4. Click on ExtendedInterface.DLL
  5. Click OK.

The Toolbox should have two new components: StatusMessage and ToolBarFunction.

The other component  StatusMessage was discussed in an earlier article.

2. Optional: Give useful names to your menus

Menu Item Properties

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.

3. Add the ToolBarFunction component to your form.

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.

4. Assign MenuItem functionality to your toolbar.

You will, of course, need to have a ToolBar on your form. Drag one over from your Toolbox if you haven't already.

  1. Display the properties of your ToolBar.
  2. Click on the 'Buttons' collection and make sure you have at least one button. Call it anything you wish, I used ToolOpen.
  3. You will see a 'ToolbarFunction' property that the component has added for you. Use the drop down list to view all of your menu items.
  4. Select the MenuItem that you want the button to use when clicked.
  5. Click OK to close the collection.

Adding ToolBar Buttons

5. Test your application

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.

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 );
         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( ); 


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


About the Author

Zombies with Coffee, LLC
Founder Zombies With Coffee, LLC
United States United States
Phillip has been a programmer long enough to remember cutting paper strip code out of Dr. Dobbs and running them thru a mechanical decoder to avoid typing in the samples.

You may also be interested in...

Comments and Discussions

Generalwant to add CasussesValidation Property to each Button of the Toolbar Pin
Tariq Rahim14-Apr-05 9:54
memberTariq Rahim14-Apr-05 9:54 
I want to add the CasusesValidation Property to each Button of the Toolbar so that i can validate the other controls.
how i will do please tell me in deatil

GeneralAn alternative approach from msdn Pin
Willem_G26-Aug-04 4:36
memberWillem_G26-Aug-04 4:36 
GeneralText on Toolbar button not shown Pin
Boogie3-Nov-03 18:20
memberBoogie3-Nov-03 18:20 
General.Net 2003 made the code unusable... Pin
psdavis28-Oct-03 3:35
memberpsdavis28-Oct-03 3:35 
Generalorder in InitializeComponent Pin
Anonymous23-Sep-02 0:01
sussAnonymous23-Sep-02 0:01 
GeneralRe: order in InitializeComponent Pin
Frank Hileman31-Oct-02 8:57
sussFrank Hileman31-Oct-02 8:57 
GeneralRe: order in InitializeComponent Pin
Heath Stewart23-Jan-03 2:42
memberHeath Stewart23-Jan-03 2:42 
GeneralRe: order in InitializeComponent Pin
roelang@hotmail.com25-Sep-03 20:29
memberroelang@hotmail.com25-Sep-03 20:29 
GeneralRe: order in InitializeComponent Pin
Heath Stewart26-Sep-03 1:18
editorHeath Stewart26-Sep-03 1:18 
GeneralMultiple Toolbars Pin
psdavis23-Jul-02 3:54
memberpsdavis23-Jul-02 3:54 
GeneralGreat! Pin
dolphinegd19-Sep-03 4:40
memberdolphinegd19-Sep-03 4:40 
GeneralRe: Multiple Toolbars Pin
svkoli28-Sep-03 5:17
membersvkoli28-Sep-03 5:17 
GeneralRe: Multiple Toolbars Pin
psdavis29-Sep-03 2:26
memberpsdavis29-Sep-03 2:26 
GeneralRe: Multiple Toolbars Pin
Le_MuLoT26-Sep-05 4:03
memberLe_MuLoT26-Sep-05 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180615.1 | Last Updated 11 Apr 2002
Article Copyright 2002 by Zombies with Coffee, LLC
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid