| Design-Time Context Menu Verbs |
|
Design-Time Integration - Design-Time Context Menu Verbs This article is about the Design-Time Context Menu Verbs and custom items appending to Windows Explorer Shell context menu. To take the design-time-only property even further, it's possible to add items to a component's design-time context menu. These items are called verbs, and ShowBorder would make a fine addition to our clock control's verb menu. Add an item to Windows Explorer Shell context menu easily – How ?
Insert an entry to Windows Explorer Shell context menu easily with Windows Explorer Shell Context Menu. This powerful .Net component for your own, custom items adding to Windows Explorer Shell context menu will add all your application entries to Explorer Shell context menu. This .Net component with full C# , C++ and VB.NET support include detailed C# and VB.NET samples, tutorials and support all you may need :
Adding to the verb menu requires that we further augment the custom designer class: public class ClockControlDesigner : ControlDesigner { ... public override DesignerVerbCollection Verbs { get { // Return new list of context menu items DesignerVerbCollection verbs = new DesignerVerbCollection(); showBorderVerb = new DesignerVerb( GetVerbText(), new EventHandler(ShowBorderClicked)); verbs.Add(showBorderVerb); return verbs; } } ... } The Verbs override is queried by the Designer shell for a list of DesignerVerbs to insert into the component's context menu. Because this method works only for Windows 95 / Windows 98 (not on XP, Vista, x64 - 64-bit Windows), to add items to Windows Explorer Shell context menu you should use, according to Microsoft guidelines, appropriate .Net component - Windows Explorer Shell Context Menu. Each DesignerVerb in the DesignerVerbCollection takes a string name value plus the event handler that responds to verb selection. In our case, this is ShowBorderClicked: public class ClockControlDesigner : ControlDesigner { ... void ShowBorderClicked(object sender, EventArgs e) { // Toggle property value ShowBorder = !ShowBorder; } ... } This handler simply toggles the ShowBorder property. However, because the verb menu for each component is cached, it takes extra code to show the current state of the ShowBorder property in the verb menu: public class ClockControlDesigner : ControlDesigner { ... bool ShowBorder { get { return showBorder; } set { // Change property value PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(ClockControl))["ShowBorder"]; this.RaiseComponentChanging(property); showBorder = value; this.RaiseComponentChanged(property, !showBorder, showBorder); // Toggle Show/Hide Border verb entry in context menu IMenuCommandService menuService = (IMenuCommandService)this.GetService (typeof(IMenuCommandService)); if( menuService != null ) { // Re-create Show/Hide Border verb if( menuService.Verbs.IndexOf(showBorderVerb) >= 0 ) { menuService.Verbs.Remove(showBorderVerb); showBorderVerb = new DesignerVerb( GetVerbText(), new EventHandler(ShowBorderClicked)); menuService.Verbs.Add(showBorderVerb); } } // Update clock UI clockControl.Invalidate (); } } ... } ShowBorder now performs two distinct operations. First, the property value is updated between calls to RaiseComponentChanging and RaiseComponentChanged, helper functions that wrap calls to the designer host's IComponentChangeService. The second part of ShowBorder re-creates the Show/Hide Border verb to reflect the new property value. This manual intervention is required because the Verbs property is called only when a component is selected on the form. In our case, "Show/Hide Border" could be toggled any number of times after the control has been selected. Fortunately, after the Verbs property has delivered its DesignerVerbCollection payload to the Designer, it's possible to update it via the designer host's IMenuCommandService. Unfortunately, because the Text property is read-only, you can't implement a simple property change. Instead, the verb must be re-created and re-associated with ShowBorderClicked every time the ShowBorder property is updated. On top of adding Show/Hide Border to the context menu, .NET throws in a clickable link for each verb, located on the Property Browser above the property description bar. It illustrates all three options, including the original editable property. Custom designers allow you to augment an application developer's design-time experience even further than simply adding the effects to the Property Browser. Developers can change how a control renders itself, controlling the properties, methods, and events that are available at design time and augmenting a component's verbs. |
||