Tool Strips


As an application offers a greater number of commands, the associated menu items can become more deeply hidden within a burgeoning menu strip hierarchy. This means that users likely spend more time searching for a specific command. If you encounter this situation, you can save users time by providing access to the most commonly used commands from a tool strip, which displays them as single-click buttons.

You can add a tool strip to a form by dragging a ToolStrip control onto it from the Toolbox. As with the MenuStrip control, you can manually add items on the design surface, or you can insert a standard set of tool strip buttons automatically by selecting "Insert Standard items" from the ToolStrip's smart tag, resulting in the form shown in Figure 2.27.

Figure 2.27. ToolStrip with Standard Tool Strip Buttons Inserted


ToolStrip is a container for zero or more tool strip items, which are stored in the Items property of type ToolStripItemCollection. The Windows Forms Designer provides a rich selection of tool strip items for the ToolStrip, including ToolStripButton, ToolStripLabel, ToolStripSplitButton, ToolStripDropDownButton, ToolStripSeparator, ToolStripComboBox, ToolStripTextBox, and ToolStripProgressBar.

The most common item is ToolStripButton, which exposes the following interesting design time properties and events:

namespace System.Windows.Forms {   class ToolStripButton : ToolStripItem {     // Properties     ToolStripItemAlignment Alignment { get; set; }     bool AutoSize { get; set; }     bool AutoToolTip { get; set; }     bool Checked { get; set; }     bool CheckOnClick { get; set; }     CheckState CheckState { get; set; }     ToolStripItemDisplayStyle DisplayStyle { get; set; }     virtual bool Enabled { get; set; }     virtual Image Image { get; set; }     ContentAlignment ImageAlign { get; set; }     MergeAction MergeAction { get; set; }     int MergeIndex { get; set; }     ToolStripItemPlacement Placement { get; }     virtual bool Pressed { get; }     virtual bool Selected { get; }     virtual string Text { get; set; }     virtual ContentAlignment TextAlign { get; set; }     virtual ToolStripTextDirection TextDirection { get; set; }     TextImageRelation TextImageRelation { get; set; }     string ToolTipText { get; set; }     bool Visible { get; set; }     // Events     event EventHandler CheckedChanged;     event EventHandler CheckStateChanged;     event EventHandler Click;     event EventHandler DoubleClick;     event MouseEventHandler MouseDown;     event EventHandler MouseEnter;     event EventHandler MouseHover;     event EventHandler MouseLeave;     event MouseEventHandler MouseMove;     event MouseEventHandler MouseUp;   } }


Here are the properties you'll encounter most often:

  • Image and ImageAlign display an image and align it within the displayable area.

  • Text lets you show text. (As discussed earlier, you can use an "&" (ampersand) to help users with keyboard menu navigation.)

  • TextAlign governs how the text is aligned.

  • TextImageRelation specifies where text should appear in relation to an image.

  • DisplayStyle lets you choose whether to display text or an image or both.

  • Checked enables toggle-style button support.

  • Enabled and Visible determine whether the item can be chosen or whether it will be shown, respectively.

  • MergeAction and MergeIndex are MDI-related (discussed later in this chapter).

As with ToolStripMenuItem, the Click event handler is likely the most often used because it is fired whenever a tool strip button is clicked:

void newToolStripButton_Click(object sender, EventArgs e) {   MessageBox.Show("Out with the old, in with the new!", "File | New"); }





Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net