在自定义表单上添加 DesignerVerbs
Add DesignerVerbs on a custom Form
是否可以在 自定义表单 上添加 DesignerVerbs
?我尝试为我的 自定义表单 class 创建一个 自定义设计器 class 并像这样使用它...
<Designer(GetType(CustomDesigner))>
Public Class CustomForm
Inherits Form
'...
End Class
我也尝试像这样将所有 "work" 放入我的 自定义表单的 class...
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
但运气不好。
如果您要向 Form
的设计器添加一些自定义动词,您需要通过派生自 DocumentDesigner
并覆盖大量属性来创建新的自定义 Designer
和重新创建 FormDesigner
.
的方法
作为一种更简单的解决方案,您可以调整表单基本表单的设计器。比方说,你有 Form1
并且你想为它使用 Do Something
动词。为此,如果 BaseForm
是 Form1
的基本形式,只需将以下代码添加到 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
:
的上下文菜单中
如果您想走更难的路,您可以在这里找到 FormDocumentDesigner
which is derived from DocumentDesigner
.
的源代码
是否可以在 自定义表单 上添加 DesignerVerbs
?我尝试为我的 自定义表单 class 创建一个 自定义设计器 class 并像这样使用它...
<Designer(GetType(CustomDesigner))>
Public Class CustomForm
Inherits Form
'...
End Class
我也尝试像这样将所有 "work" 放入我的 自定义表单的 class...
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
但运气不好。
如果您要向 Form
的设计器添加一些自定义动词,您需要通过派生自 DocumentDesigner
并覆盖大量属性来创建新的自定义 Designer
和重新创建 FormDesigner
.
作为一种更简单的解决方案,您可以调整表单基本表单的设计器。比方说,你有 Form1
并且你想为它使用 Do Something
动词。为此,如果 BaseForm
是 Form1
的基本形式,只需将以下代码添加到 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
:
如果您想走更难的路,您可以在这里找到 FormDocumentDesigner
which is derived from DocumentDesigner
.