Library tutorials & articles

Introduction to Designers

Adding Verbs

The last thing I will cover in this article is designer verbs. These are actions that are presented on the control's context menu and also as hyperlinks in the property grid when your component is selected, and allow you to provide an interface for the user for performing common tasks with your control.

To implement designer verbs, you must override the Verbs property and return a DesignerVerbCollection object. Each designer verb is presented as a menu option and as a hyperlink in the property grid, and is associated with an event handler that is called when it is selected. Let's try adding one that simply shows a message box when chosen.

[VB]
Imports System.ComponentModel.Design
Public Overrides ReadOnly Property Verbs() As _
System.ComponentModel.Design.DesignerVerbCollection
    Get
        Dim v As New DesignerVerbCollection()
        v.Add(New DesignerVerb("Sample Verb", AddressOf SampleVerbHandler))
        Return v
    End Get
End Property
Private Sub SampleVerbHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show("You clicked the test designer verb!")
End Sub

[C#]
using System.ComponentModel.Design;
public override System.ComponentModel.Design.DesignerVerbCollection Verbs
{
    get
    {
        DesignerVerbCollection v = new DesignerVerbCollection();
        v.Add(new DesignerVerb("Sample Verb", new EventHandler
        (SampleVerbHandler)));
        return v;
    }
}
private void SampleVerbHandler(object sender, System.EventArgs e)
{
    MessageBox.Show("You clicked the test designer verb!");
}

When your control is selected on the design surface, our hyperlink is now shown in the property grid.

Conclusion

Designers are a great way of adding that extra touch to any control, to make the developer's life that much easier when configuring it at design time. I'm aware I've only scratched the surface in this article, but I intend to go much deeper in future tutorials. I hope I've shown you enough to get you started writing your own designers. Consult the documentation for the other overridable members of the various designer classes, and don't be afraid to experiment!

I've provided all the source code covered in this article in both VB and C# projects. You'll have to create a test application and add the control in the compiled assembly to the toolbox to test them.

Comments

  1. 06 Apr 2004 at 05:53

    I'm just going through this article and I've found that the CanBeParentedTo is not called when the control is just dragged from the tool box. It is called only when the control is already sited on a form and then it is dragged into a Panel or GroupBox etc.
    I think it's worth to mention it. I've just spent an hour experimenting
    Arthur

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Introduction to Designers.

Leave a comment

Sign in or Join us (it's free).

Tim Dawson

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!