Extended Interface for Toolbars

By psdavis
Posted 11 Apr 2002
38,858 views

Articles by this authoropen in new window

Download project and source - 16 Kbopen in new window

Problem

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.

Background

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

About psdavis

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.

Click here to view psdavis's online profile.

Contributors: FHL