【问题标题】:Add DesignerVerbs on a custom Form在自定义表单上添加 DesignerVerbs
【发布时间】:2018-05-27 10:47:13
【问题描述】:

是否可以在自定义表单上添加DesignerVerbs?我尝试为我的自定义表单类制作一个自定义设计器类并像这样使用它......

<Designer(GetType(CustomDesigner))>
Public Class CustomForm
    Inherits Form
    '...
End 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

但没有运气。

【问题讨论】:

  • 为什么CustomForm中有Verbs
  • @RezaAghaei:在这种情况下,用于大规模恢复某些属性。
  • 您想在设计器中显示的动词应该来自您为表单注册的设计器类。您需要发布CustomDesigner 代码。
  • @RezaAghaei:我已经创建了一个带有我想要显示的动词的自定义设计器,我从我的自定义表单的类中“调用”了这个自定义设计器类,但是什么都没有!!!
  • 这也与您的另一个问题Hide form's DoubleBuffered property without make it nonfunctional 有关,该问题使用相同的技术来避免编写自定义设计器。

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


【解决方案1】:

如果您要为Form 的设计器添加一些自定义动词,您需要通过从DocumentDesigner 派生并覆盖许多属性和方法来重新创建FormDesigner 来创建一个新的自定义Designer

作为一种更简单的解决方案,您可以调整表单基本表单的设计器。比方说,你有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的源代码,它源自DocumentDesigner

【讨论】:

  • 非常感谢 Reza,它 100% 有效!!!顺便问一下,如果我想努力学习,有什么链接或东西吗?
  • 不客气 :) 在这里您可以找到源自DocumentDesignerFormDocumentDesigner 的源代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-19
相关资源
最近更新 更多