9.10 Extending the Design Time Context Menu

 <  Day Day Up  >  

You want to add a new item to the context menu of a control during design time.


Technique

The context menu is built from a collection of objects known as verbs . These verbs are defined in the DesignerVerbCollection of a control designer. Create a class derived from System.Windows.Forms.Design.ControlDesigner and override the Verbs property. The getter method of the Verbs property returns a DesignerVerbCollection object. Within the implementation of the Verbs getter method, create a new DesignerVerbCollection object and call its Add method for each verb you want to add. The Add method accepts two parameters: a string parameter used as the display string for the menu item and an EventHandler delegate, which is called when the verb is selected by the user :

 
 public override System.ComponentModel.Design.DesignerVerbCollection Verbs {     get     {         DesignerVerbCollection v = new DesignerVerbCollection();         v.Add(new DesignerVerb("Randomize", new EventHandler(RandomizeVerbHandler)));         return v;     } } private void RandomizeVerbHandler(object sender, System.EventArgs e) {     ((RandomShape)this.Control).Randomize(); } 

Comments

The last recipe showed how to act in concert with the forms designer to alter the behavior of a control during design time. The code associated with the last recipe, which you can download from the publisher's Web site http://www.samspublishing.com, created a custom control that simply draws a specified number of random shapes of differing colors. The drawing area of the control is used as a rough indication for each shape's location. In other words, a shape is always guaranteed to draw mainly on the control. However, if the control is resized in any direction, the locations and sizes of each drawn shape are not recalculated. This anomaly fits well into the discussion of extending the design-time context menu using verbs.

A verb is an action associated with a display string. Even though we mentioned that you can extend the context menu using verbs, each verb is also placed into the property browser of a control as a blue hyperlink, as shown in Figure 9.4. When a user selects the context menu item associated with a verb or clicks on the hyperlink within the property browser, the verb's associated event handler method is called. In the case of the RandomShapeControl , the Randomize event is called. Before this verb was used, the user would have to force the randomize method by either rebuilding the project or forcing the form to repaint somehow during design time. By adding the Randomize verb, you let the user change the size of the control and then select the verb without having to force the shape randomization.

Figure 9.4. Adding items to the designer context menu.

graphics/09fig04.jpg

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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