在自定义表单

时间:2018-05-27 10:47:13

标签: .net vb.net winforms custom-controls windows-forms-designer

是否可以在自定义表单上添加DesignerVerbs?我已尝试为自定义表单类创建自定义设计器类,并像这样使用它...

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

我也尝试做所有&#34;工作&#34;进入我的自定义表格,就像这样......

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

但没有运气。

1 个答案:

答案 0 :(得分:3)

如果要向Form的设计人员添加一些自定义谓词,则需要通过派生Designer并覆盖大量属性和方法来创建新的自定义DocumentDesigner重新创建FormDesigner

作为一种更简单的解决方案,您可以调整表单基本形式的设计者。比方说,你有Form1,你想要Do Something动词。为此,如果BaseFormForm1的基本表单,则只需将以下代码添加到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!");
    }));
}

因此, Do Something 将添加到Form1的上下文菜单中:

enter image description here

如果你想更加努力,可以在这里找到源自FormDocumentDesignerDocumentDesigner的源代码。