ToolStrip Control Architecture
The ToolStrip and ToolStripItem classes provide a flexible, extensible system for displaying toolbar, status, and menu items. These classes are all contained in the System.Windows.Forms namespace and they are all typically named with the "ToolStrip" prefix (such as ToolStripOverflow) or with the "Strip" suffix (such as MenuStrip).
The following topics describe ToolStrip and the controls that derive from it.
You can access all the items in a ToolStrip through the Items collection. You can access all the items in a ToolStripDropDownItem through the DropDownItems collection. In a class derived from ToolStrip, you can also use the DisplayedItems property to access only those items that are currently displayed. These are the items that are not currently in an overflow menu.
The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the ToolStrip control:
MenuStrip is the top-level container that supersedes MainMenu. It also provides key handling and multiple document interface (MDI) features. Functionally, ToolStripDropDownItem and ToolStripMenuItem work along with MenuStrip, although they are derived from ToolStripItem.
The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the MenuStrip control:
StatusStrip replaces the StatusBar control. Special features of StatusStrip include a custom table layout, support for the form's sizing and moving grips, and the
Spring property, which allows a ToolStripStatusLabel to fill available space automatically.
The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the StatusStrip control:
ContextMenuStrip replaces ContextMenu. You can associate a ContextMenuStrip with any control, and a right mouse click automatically displays the context menu (or shortcut menu). You can show a ContextMenuStrip programmatically by using the Show method. ContextMenuStrip supports cancelable Opening and Closing events to handle dynamic population and multiple-click scenarios. ContextMenuStrip supports images, menu-item check state, text, access keys, shortcuts, and cascading menus.
The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the ContextMenuStrip control:
ToolStrip Generic Features
The following topics describe features and behavior that are generic to the ToolStrip and derived controls.
You can do custom painting in ToolStrip controls in several ways. As with other Windows Forms controls, the ToolStrip and ToolStripItem both have overridable
OnPaint methods and
Paint events. As with regular painting, the coordinate system is relative to the client area of the control; that is, the upper left-hand corner of the control is 0, 0. The
Paint event and
OnPaint method for a ToolStripItem behave like other control paint events.
The ToolStrip controls also provide finer access to the rendering of the items and container through the ToolStripRenderer class, which has overridable methods for painting the background, item background, item image, item arrow, item text, and border of the ToolStrip. The event arguments for these methods expose several properties such as rectangles, colors, and text formats that you can adjust as desired.
To adjust just a few aspects of how an item is painted, you typically override the ToolStripRenderer.
If you are writing a new item and want to control all aspects of the painting, override the
OnPaint method. From within
OnPaint, you can use methods from the ToolStripRenderer.
The concept of container ownership and parenting is more complex in ToolStrip controls than in other Windows Forms container controls. That is necessary to support dynamic scenarios such as overflow, sharing drop-down items across multiple ToolStrip items, and to support the generation of a ContextMenuStrip from a control.
The following list describes members related to parenting and explains their use.
GetCurrentParent is a read-only accessor to the Parent property. A parent differs from an owner in that a parent denotes the returned current ToolStrip in which the item is displayed, which might be in the overflow area.
Owner returns the ToolStrip whose Items collection contains the current ToolStripItem. This is the best way to reference ImageList or other properties in the top-level ToolStrip without writing special code to handle overflow.
Behavior of Inherited Controls
The following controls are locked whenever they are used in inheritance:
For example, create a new Windows Forms application by using one or more of the controls in the previous list. Set the access modifier of one or more controls to
protected, and then build the project. Add a form that inherits from the first form, and then select an inherited control. The control appears locked, behaving as if its access modifier was
ToolStripContainer Support of Inheritance
The ToolStripContainer control supports limited inherited scenarios, similar to the following example:
Create a new Windows Forms application.
Add a ToolStripContainer to the form.
Set the access modifier of the ToolStripContainer to
Build the project.
Add a form that inherits from the first form.
Select the inherited ToolStripContainer on the form.
Inherited Behavior of Child Controls
After you complete the previous steps, the following inherited behavior occurs:
In the designer, the control appears with an inherited icon.
The ToolStripPanel controls are locked; you cannot select or rearrange their contents.
Your changes persist after building the form.
The limitations of
ToolStrips under partial trust are designed to prevent inadvertent entry of personal information that might be used by unauthorized persons or services. The protective measures are as follows:
ToolStripDropDowncontrols require AllWindows to display items in a ToolStripControlHost. This applies to both intrinsic controls such as ToolStripTextBox, ToolStripComboBox, and ToolStripProgressBar as well as to user-created controls. If this requirement is not met, these items are not displayed. No exception is thrown.
Setting the AutoClose property to
falseis not allowed, and the cancelable Closing event parameter is ignored. This makes it impossible to enter more than one keystroke without dismissing the drop-down item. If this requirement is not met, such items are not displayed. No exception is thrown.
Many keystroke handling events will not be raised if they occur in partial trust contexts other than AllWindows.
Access keys are not processed when AllWindows is not granted.
The following usage patterns have a bearing on ToolStrip layout, keyboard interaction, and end-user behavior:
Joined in a ToolStripPanel
The ToolStrip can be repositioned within the ToolStripPanel and across ToolStripPanels. The
Dockproperty is ignored, and if the Stretch property is
false, the size of the ToolStrip grows as items are added to the ToolStripPanel. Typically, the ToolStrip does not participate in the tab order.
The ToolStrip is placed on one side of a container in a fixed position, and its size expands over the entire edge to which it is docked. Typically, the ToolStrip does not participate in the tab order.
Combined with or following the ALT key, access keys are one way to activate a control using the keyboard. ToolStrip supports both explicit and implicit access keys. Explicit definition uses an ampersand (&) character preceding the letter. Implicit definition uses an algorithm that attempts to find a matching item based on the order of characters in a given
The shortcut keys used by a MenuStrip use a combination of the Keys enumeration (which is not order-specific) to define the shortcut key. You can also use the ShortcutKeyDisplayString property to display a shortcut key with text only, such as displaying "Del" instead of "Delete."
The ALT key activates the MenuStrip pointed to by MainMenuStrip. From there, CTRL+TAB navigates between ToolStrip controls within
ToolStripPanels. The TAB key and the arrow keys on the numeric keypad navigate between items in a ToolStrip. A special algorithm handles navigation in the overflow region. SPACEBAR selects a ToolStripButton, ToolStripDropDownButton, or ToolStripSplitButton.
Focus and Validation
When activated by the ALT key, the MenuStrip or ToolStrip typically neither take nor remove the focus from the control that currently has the focus. If there is a control hosted within the MenuStrip or a drop-down of the MenuStrip, the control gains focus when the user presses the TAB key. In general, the GotFocus, LostFocus, Enter, and Leave events of MenuStrip might not be raised when they are activated by the keyboard. In such cases, use the MenuActivate and MenuDeactivate events instead.
Stacking is the arranging of items beside each other at both ends of the ToolStrip. The following list describes the stack layouts.
Other Features of Stack Layouts
When items do not fit within the ToolStrip, an overflow button automatically appears. The Overflow property setting determines whether an item appears in the overflow area always, as needed, or never.
In the LayoutCompleted event, you can inspect the Placement property to determine whether an item was placed on the main ToolStrip, the overflow ToolStrip, or if it is not currently showing at all. The typical reasons why an item is not displayed are that the item did not fit on the main ToolStrip and its Overflow property was set to Never.
Other Layout Options
The Alignment property is ignored.
The Alignment property is ignored.
The following topics describe ToolStripItem and the controls that derive from it.
ToolStripItem controls must be contained in a ToolStrip, MenuStrip, StatusStrip, or ContextMenuStrip and cannot be added directly to a form. The various container classes are designed to contain an appropriate subset of ToolStripItem controls.
The following table lists the stock ToolStripItem controls and the containers in which they look best. Although any ToolStrip item can be hosted in any ToolStrip-derived container, these items were designed to look best in the following containers:
ToolStripDropDown does not appear in the designer toolbox.
ToolStripButton is the button item for ToolStrip. You can display it with various border styles, and you can use it to represent and activate operational states. You can also define it to have the focus by default.
The ToolStripLabel provides label functionality in ToolStrip controls. The ToolStripLabel is like a ToolStripButton that does not get focus by default and that does not render as pushed or highlighted.
ToolStripLabel as a hosted item supports access keys.
The ToolStripSeparator adds a vertical or horizontal line to a toolbar or menu, depending on the orientation. It provides grouping of or distinction between items, such as those on a menu.
You can add a ToolStripSeparator at design time by choosing it from a drop-down list. However, you can also automatically create a ToolStripSeparator by typing a hyphen (-) in either the designer template node or in the Add method.
ToolStripControlHost is the abstract base class for ToolStripComboBox, ToolStripTextBox, and ToolStripProgressBar. ToolStripControlHost can host other controls, including custom controls, in two ways:
Extend ToolStripControlHost, and in the inherited class's parameterless constructor, call the base class constructor passing a class that derives from Control. This option lets you wrap common control methods and properties for easy access in a ToolStrip.
ToolStripComboBox is the ComboBox optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripComboBox level, but the underlying ComboBox control is fully accessible through the ComboBox property.
ToolStripTextBox is the TextBox optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripTextBox level, but the underlying TextBox control is fully accessible through the TextBox property.
ToolStripProgressBar is the ProgressBar optimized for hosting in a ToolStrip. A subset of the hosted control's properties and events are exposed at the ToolStripProgressBar level, but the underlying ProgressBar control is fully accessible through the ProgressBar property.
ToolStripDropDownItem is the abstract base class for ToolStripMenuItem, ToolStripDropDownButton, and ToolStripSplitButton, which can host items directly or host additional items in a drop-down container. You do this by setting the DropDown property to a ToolStripDropDown and setting the Items property of the ToolStripDropDown. Access these drop-down items directly through the DropDownItems property.
ToolStripDropDownButton looks like ToolStripButton, but it shows a drop-down area when the user clicks it. Hide or show the drop-down arrow by setting the ShowDropDownArrow property. ToolStripDropDownButton hosts a ToolStripOverflowButton that displays items that overflow the ToolStrip.
ToolStripSplitButton combines button and drop-down button functionality.
ToolStripItem Generic Features
ToolStripItem provides the following generic features and options to inheriting controls:
Text and image relationship
ToolStripItem controls receive their own click, mouse, and paint events, and can perform some keyboard preprocessing also.
The Image, ImageAlign, ImageIndex, ImageKey, and ImageScaling properties pertain to various aspects of image handling. Use images in ToolStrip controls by setting these properties directly or by setting the run-time–only ImageList property.
The value of the Alignment property determines the end of the ToolStrip at which an item appears. The Alignment property works only when the layout style of the ToolStrip is set to one of the stack overflow values.
Items are placed on the ToolStrip in the order in which the items appear in the Items collection. To programmatically change where an item is laid out, use the Insert method to move the item in the collection. This method moves the item but does not duplicate it.
Text and Image Relationship
The TextImageRelation property defines the relative placement of the image with respect to the text on a ToolStripItem. Items that lack an image, text, or both are treated as special cases so that the ToolStripItem does not display a blank spot for the missing element or elements.
DisplayStyle allows you to set the values of an item's Text and Image properties while displaying only what you want. This is typically used to change only the display style when showing the same item in a different context.
Classes that provide various other functionality include:
ToolStripContainer is similar to SplitContainer. It uses four docked side panels (instances of ToolStripPanel) and one central panel (an instance of ToolStripContentPanel) to create a typical arrangement. You cannot remove the side panels, but you can hide them. You can neither remove nor hide the central panel. You can arrange one or more ToolStrip, MenuStrip, or StatusStrip controls in the side panels, and you can use the central panel for other controls. The ToolStripContentPanel also provides a way to get renderer support into the body of your form for a consistent appearance. ToolStripContainer does not support multiple document interface (MDI).