Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / XML

Adding a Custom Control to a ToolStripDropDownButton

Rate me:
Please Sign up or sign in to vote.
4.70/5 (13 votes)
9 Oct 20062 min read 89.3K   2.4K   80   6
One approach to displaying a custom control in a ToolStrip drop-down button

Sample Image - ToolStripDropDown.jpg

Introduction

This article demonstrates one way of extending ToolStripDropDown to display a custom control in a drop-down button of a ToolStrip.

While a 'Table Sizer' control (similar to that used in the MS-Word toolbar) is used in the example, the article is primarily intended as a starting point for those wanting to extend ToolStripDropDown.

Background

The following diagram depicts some of the classes typically used to implement a ToolStrip:

Image 2

Note that the items of a ToolStrip (i.e., ToolStripItem-derived elements) are Components, not Controls. To add a Control-derived object to a ToolStrip (or, in our case, a ToolStripDropDown) we use the TooStripControlHost class.

Using TooStripControlHost to host our custom control, we end up with the following design:

Image 3

Any configuring and/or wiring up of events required for the TableSizeControl instance can be performed through the public Selector property of the ToolStripTableSizeSelector class.

Usage

Using the drop down is mostly a matter of creating the drop-down and assigning it to the drop-down button on the ToolStrip:

C#
public Form1()
{
    InitializeComponent();

    ToolStripTableSizeSelector dropDown = new ToolStripTableSizeSelector();

    dropDown.Opening += new CancelEventHandler(DropDown_Opening);
    dropDown.Selector.TableSizeSelected +=
        new TableSizeSelectedEventHandler(Selector_TableSizeSelected);

    this.tableSizerDropDownButton.DropDown = dropDown;
}

Note: The Opening event is hooked up here to reset the state of the TableSizeControl before it is displayed.

Points of Interest

There were several 'sticking points' in presenting a custom control as a drop-down item:

  • Ensuring the background colour of the hosted control matched the colour scheme of the ToolStrip
  • Setting the focus to the custom control when the drop-down is first displayed

For want of a better solution, these were addressed via the OnOpening and OnOpened events of the ToolStripDropDown class.

Closing the drop-down at the appropriate time was achieved by hooking up the 'selected' and 'cancelled' events of the custom control when it is first created. As an alternative, ToolStripControlHost offers the protected virtuals: OnSubscribeControlEvents and OnUnsubscribeControlEvents. Perhaps, this would have been a better place to wire up the events, although it wasn't clear from the documentation what the benefit of this approach is.

References

Below are additional articles on working with the ToolStripControlHost class:

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
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
Murugesan.sam2-May-12 2:04
Murugesan.sam2-May-12 2:04 
GeneralHI Tony Ioanides Pin
chenli051320-Jan-09 20:49
chenli051320-Jan-09 20:49 
Generali found a problem Pin
chenli051320-Jan-09 20:43
chenli051320-Jan-09 20:43 
Generalgreate article [modified] Pin
chenli051313-Jan-09 2:31
chenli051313-Jan-09 2:31 
Generaldatetime picker Pin
yoav sagi25-Feb-07 2:29
yoav sagi25-Feb-07 2:29 
GeneralNice article Pin
Erik Vullings10-Dec-06 19:18
Erik Vullings10-Dec-06 19:18 

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.