Simos Sigma
Simos Sigma

Reputation: 978

Add DesignerVerbs on a custom Form

Is it possible to add DesignerVerbs on a custom form? I have try to make a custom designer class for my custom form class and use it like this...

<Designer(GetType(CustomDesigner))>
Public Class CustomForm
    Inherits Form
    '...
End Class

I have also try to do all the "work" into my custom form's class like this...

Imports System.ComponentModel.Design

Public Class CustomForm
    Inherits Form
    '...
    Private _Verbs As DesignerVerbCollection
    Public ReadOnly Property Verbs() As DesignerVerbCollection
        Get
            If _Verbs Is Nothing Then
                _Verbs = New DesignerVerbCollection From {
                New DesignerVerb("Verb1", New EventHandler(AddressOf EventHandler1)),
                New DesignerVerb("Verb2", New EventHandler(AddressOf EventHandler2))
                }
                _Verbs(0).Visible = False
                _Verbs(1).Visible = True
            End If
            Return _Verbs
        End Get
    End Property
    Private Sub EventHandler1(ByVal sender As Object, ByVal e As EventArgs)
        '...
    End Sub
    Private Sub EventHandler2(ByVal sender As Object, ByVal e As EventArgs)
        '...
    End Sub
End Class

But with no luck.

Upvotes: 2

Views: 189

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125207

If you are going to add some custom verbs to designer of a Form, you need to create a new custom Designer by deriving from DocumentDesigner and overriding a lot of properties and method to recreate FormDesigner.

As an easier solution, you can tweak designer of base form of your form. Let's say, you have Form1 and you want to have Do Something verb for it. To do so, if BaseForm is the base form for your Form1, it's enough to add the following code to BaseForm:

//You may want to add null checking to the code.

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    if (!DesignMode)
        return;
    var host = (IDesignerHost)this.Site.GetService(typeof(IDesignerHost));
    var designer = host.GetDesigner(this);
    designer.Verbs.Add(new DesignerVerb("Do Something", (obj, args) =>
    {
        MessageBox.Show("Something done!");
    }));
}

As a result, Do Something will be added to context menu for your Form1:

enter image description here

If you like to go the harder way, here you can find the source code for FormDocumentDesigner which is derived from DocumentDesigner.

Upvotes: 4

Related Questions