Context menu strips

Context menu strips

1.     Context menus provide quick access to commands immediately relevant to what the user is currently trying to accomplish or understand.

2.     Most controls in the System.Windows.Forms namespace support the ContextMenuStrip property inherited from the Control class to specify a ContextMenuStrip object to associate with the control.

3.     This setting can be changed dynamically to allow different context menus to display depanding on the state of the control.

4.     The ContextMenuStrip class is essentially a container for the ToolStripItem that appear within the munu.

5.     While it is certainly posible to re-create the same context menu within our menu strip, it does not seem like the most elegant solution. Having this menu in two places would require that we update one menu when we make any change to the other. Ideally, we would prefer to have a single menu, and find a way to share this menu between the two strip objects.

Drop-down events and event arguments

1.     We would prefer to process all submenu items in a single event handler. There are a couple of reasons for this. First, of course, it provides an alternate example for this fine book you are reading.More importantly, it encapsulates the logic for these menus in a single handler. If we later need to make a change or add a new submenu item, we only need to modify this one handler.

2.     The ToolStripMenuItem class supports various drop-down events before, during,and after such a submenu is displayed.This permits an event handler to modify the contents or appearance of the submenu as dictated by the application. An example of this concept can be found in the windows operating system.Open the My Computer window and display the File menu. The contents of this menu change depending on the type of file currently selected.

3.     To facilitate this amazing behavior, we begin by employing the Tag property in our menus to record the desired SizeMode value for each menu.

4.     The Tag property is available in most Windows Forms components, including ToolStripItem objects, and allows pretty much anything to be associated with the component. The control class defines this property for all Windows Forms controls.The property is of type object, so any class, structure, or other value can be assigned.

5.     All .NET event handlers employ a common set of arguments. The first parameter is the object sending the event, while the second parameter is the event argument object.

6.     The C# as keyword converts a variable to a given reference type. If the variable cannot be converted to the given type, then null is returned.

7.     The .NET Framework contains a special Enum structure that is the implicit base class for all enumerations.This class contains various static methods for manipulating enumeration values and their corresponding strings.

Altering a submenu before it appears

1.     A more interface would highlight the current selection in the submenu. this would immediately indicate what mode is currently displayed, and help our user make a more informed selection.

2.     The ToolStripMenuItem class provides a Checked property that, when true, display a check mark next to the menu. Of course ,as our program changes, there might be other commands or user interacitons that alter the display mode of the image, so this approach could get complicaqted.

3.     The DropDownOpening event is designed for just this purpose. This event occurs just before the drop-down list associated with an item displayed, allowing its appearance or contents to be modified and then immediately displayed to the user.

Status strips

1.     A status bar at the base of a window is a good place for this type of data, as it provides quick feedback related to the current task or cursor position.

2.     In .NET , status bars are represented by the StatusStrip class, which is part of the ToolStrip class hierarchy we have been discussing.

3.     The StatusStrip class represents a status bar, and contains one or more ToolStripStautsLabel objects.

4.     The toolStripLabel class overrides the CanSelect property from the ToolStripItem class to enforce that such labels cannot be selected.

 

posted on 2009-10-14 16:06  韩连生  阅读(372)  评论(0)    收藏  举报

导航