This post is simple and neat, the goal now is to add some designer verbs, so lets get straight.
What is a Designer Verb?
A designer can use the DesignerVerb class to add menu commands to the shortcut menu for the component it provides designer support for in design mode. Designer verbs associate a menu item with an event handler. Designer verbs are provided to the design-time environment by the Verbs property of the IDesigner interface. [more]
I will use the sample I introduced in the last post, I have a TestUserControl that has a red dotted border , and a ShowBorder property to show or hide that border, now I will add on the UserControlDesigner class some verbs ( menu items ) to change the color of the Border ( in case if we had a from with red background so the border will be lost ).
[code:c#]
public override DesignerVerbCollection Verbs
{
get
{
return new DesignerVerbCollection(
new DesignerVerb[]
{
new DesignerVerb("Make Border Red",new EventHandler(MakeRedBorder)),
new DesignerVerb("Make Border Green",new EventHandler(MakeGreenBorder)),
new DesignerVerb("Make Border Blue",new EventHandler(MakeBlueBorder)),
}
);
}
}
private void MakeRedBorder(object sender, EventArgs e)
{
_pen.Color = Color.Red;
Control.Refresh();
}
private void MakeGreenBorder(object sender, EventArgs e)
{
_pen.Color = Color.Green;
Control.Refresh();
}
private void MakeBlueBorder(object sender, EventArgs e)
{
_pen.Color = Color.Blue;
Control.Refresh();
}
[/code]
The verbs will have to places to appear now.
1. In the context menu on the control, on right click.
2. In the Property window when the control is selected, but make sure that the commands is enabled in the property window.
More from msdn: http://msdn2.microsoft.com/en-us/library/xfk1zfw9.aspx
Leave a Reply