Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 43,020 times

Contents

Downloads

Related Categories

Introduction to Designers - Adding Verbs

divil

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

  • CanBeParentedTo is not called when dragging from t

    Posted by Arthur on 06 Apr 2004

    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 f...