是否可以在自定义表单类上添加designerverbs? [英] Is it possible to add designerverbs on a custom form class?

查看:122
本文介绍了是否可以在自定义表单类上添加designerverbs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在自定义表单类上添加 DesignerVerbs ?我尝试为自定义表单类创建自定义设计器类,并像这样使用 [1] 。我也尝试将所有工作完成到我的自定义表单的类中,就像这样 [2] 。但没有运气。知道我该怎么办?如果有可能......



我尝试过:



[1]

< Designer( GetType (CustomDesigner)) > 
公共 CustomForm
继承表格
' ...
结束



[2]

  Imports  System.ComponentModel.Design 

公开 CustomForm
继承表格
< span class =code-comment>' ...
私人 _Verbs 作为 DesignerVerbCollection
公共 ReadOnly 属性 Verbs()作为 DesignerVerbCollection
获取
如果 _Verbs 没什么 然后
_Verbs = DesignerVerbCollection来自{
DesignerVerb( Verb1 EventHandler( AddressOf EventHandler1)),
DesignerVerb( Verb2 EventHandler( AddressOf EventHandler2))
}
_Verbs( 0 ).Visible = False
_Verbs( 1 )。Visible = True
结束 如果
返回 _Verbs
结束 获取
结束 属性
私有 Sub EventHandler1( ByVal sender As 对象 ByVal e As EventArgs)
' ...
结束 Sub
私有 Sub EventHandler2(< span class =code-keyword> ByVal sender 作为 对象 ByVal e As EventArgs)
' ...
结束 Sub
结束

解决方案

我在我的问题上得到了一个简单的解决方案 < b> 这里 stackoverflow.com

---

< a href =https://stackoverflow.com/users/3110834/reza-aghaei> Reza Aghaei :如果你要去要向Form的设计者添加一些自定义谓词,您需要通过从DocumentDesigner派生并重写大量属性和方法来重新创建FormDesigner来创建新的自定义Designer。作为一种更简单的解决方案,您可以调整表单基本形式的设计者。比方说,你有Form1,你想要它的Do Something动词。为此,如果BaseForm是Form1的基本表单,则将以下代码添加到BaseForm就足够了:

 受保护的 覆盖  Sub  OnHandleCreated( ByVal  e  As  EventArgs)
MyBase .OnHandleCreated(e)
Dim host = DirectCast Me .Site.GetService ( GetType (IDesignerHost)),IDesignerHost)
Dim designer = host.GetDesigner( Me
designer.Verbs.Add( New DesignerVerb( 做某事 Sub (obj,args)
MessageBox。展会( 完成的事情!
结束 Sub ))
结束 Sub

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

https://i.stack.imgur.com/j2L12.png


Is it possible to add DesignerVerbs on a custom form class? I have try to make a custom designer class for my custom form class and use it like this [1]. I have also try to do all the "work" into my custom form's class like this [2]. But with no luck. Any idea how can I do that? If it is possible...

What I have tried:

[1]

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


[2]

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

解决方案

I got an easy solution on my question here at stackoverflow.com.
---
Reza Aghaei: 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:

Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
	MyBase.OnHandleCreated(e)
	Dim host = DirectCast(Me.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
	Dim designer = host.GetDesigner(Me)
	designer.Verbs.Add(New DesignerVerb("Do Something", Sub(obj, args)
		MessageBox.Show("Something done!")
	End Sub))
End Sub

As a result, Do Something will be added to context menu for your Form1:
https://i.stack.imgur.com/j2L12.png


这篇关于是否可以在自定义表单类上添加designerverbs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆