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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler