15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

st<strong>and</strong>ard Controls <strong>and</strong> Components ❘ 1141<br />

The last two items in the list, ToolStripDropDownItem <strong>and</strong> ToolStripControlHost, deserve a little<br />

more discussion. ToolStripDropDownItem is the base class for ToolStripMenuItems, which are used to<br />

build the menu structure. ToolStripMenuItems are added to MenuStrip controls. As mentioned earlier,<br />

MenuStrips are derived from ToolStrip controls. This is important when it comes time to manipulate or<br />

extend menu items. Because toolbars <strong>and</strong> menus are derived from the same classes, creating a framework for<br />

managing <strong>and</strong> executing comm<strong>and</strong>s is much easier.<br />

ToolStripControlHost can be used to host other controls that do not derive from ToolStripItem.<br />

Remember that the only controls that can be directly hosted by a ToolStrip are those that are derived from<br />

ToolStripItem.<br />

menustrip<br />

The MenuStrip control is the container for the menu structure of an application. As mentioned earlier,<br />

MenuStrip is derived from the ToolStrip class. The menu system is built by adding ToolStripMenu<br />

objects to the MenuStrip. You can do this in code or in the Designer of Visual Studio. Drag a MenuStrip<br />

control onto a form in the Designer <strong>and</strong> the MenuStrip will allow the entry of the menu text directly on the<br />

menu items.<br />

The MenuStrip control has only a couple of additional properties. GripStyle uses the<br />

ToolStripGripStyle enumeration to set the grip as visible or hidden. The MdiWindowListItem property<br />

takes or returns a ToolStripMenuItem. This ToolStripMenuItem is the menu that shows all open<br />

windows in an MDI application.<br />

Contextmenustrip<br />

To show a context menu, or a menu displayed when the user right-clicks the mouse, the ContextMenu<br />

Strip class is used. Like MenuStrip, ContextMenuStrip is a container for ToolStripMenuItems<br />

objects. However, it is derived from ToolStripDropDownMenu. A ContextMenu is created the same<br />

way as a MenuStrip. ToolStripMenuItems are added, <strong>and</strong> the Click event of each item is defined to<br />

perform a specific task. Context menus are assigned to specific controls. This is done by setting the<br />

ContextMenuStrip property of the control. When the user right-clicks the control, the menu is displayed.<br />

Toolstripmenuitem<br />

ToolStripMenuItem is the class that builds the menu structures. Each ToolStripMenuItem<br />

object represents a single menu choice on the menu system. Each ToolStripMenuItem has a<br />

ToolStripItemCollection that maintains the child menus. This functionality is inherited from<br />

ToolStripDropDownItem.<br />

Because ToolStripMenuItem is derived from ToolStripItem, all of the same formatting properties apply.<br />

Images appear as small icons to the right of the menu text. Menu items can have check marks show up next<br />

to them with the Checked <strong>and</strong> CheckState properties.<br />

Shortcut keys can be assigned to each menu item. They are generally two key chords such as Ctrl+C<br />

(common shortcut for Copy). When a shortcut key is assigned, it can optionally be displayed on the menu by<br />

setting the ShowShortCutKey property to true.<br />

To be useful, the menu item has to do something when the user clicks it or uses the defined shortcut<br />

keys. The most common way is to h<strong>and</strong>le the Click event. If the Checked property is being used, the<br />

CheckStateChanged <strong>and</strong> CheckedChanged events can be used to determine a change in the checked state.<br />

Toolstripmanager<br />

Menu <strong>and</strong> toolbar structures can become large <strong>and</strong> cumbersome to manage. The ToolStripManager<br />

class provides the ability to create smaller, more manageable pieces of a menu or toolbar structure <strong>and</strong><br />

then combine them when needed. An example of this is a form that has several different controls on it.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!