在 UserControl 的属性上添加 DesignerVerb [英] Add DesignerVerb on UserControl's Properties

查看:31
本文介绍了在 UserControl 的属性上添加 DesignerVerb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:在找到我真正想要的内容后,我编辑了我最初的问题,以便更好地描述我最终想要做的事情.

EDIT: After I found what I was really looking for, I edited my initial question so to describe better what I finaly wanted to do.

我正在处理一个 UserControl,我想在它的属性上放置一个 DesignerVerb,就像 TreeView 控件一样.我怎样才能做到这一点?可能吗?

I am working on a UserControl and I want to place a DesignerVerb at it's properties, like TreeView control have. How can I do this? Is it possible?

推荐答案

好吧,这是一个简单的例子...

Well, here is a simple example...

1.如果我们还没有这样做,我们应该向System.Design添加一个引用.转到 Reference Manager >组件 >Framework 并找到 System.Design.检查并点击确定.

1. If we haven't done already, we should add a reference to System.Design. Goto Reference Manager > Assemblies > Framework and find System.Design. Check it and click OK.

2. 在我们的 UserControl 代码中,我们确保我们已经有 Imports System.ComponentModelImports System.ComponentModel.设计参考.

2. Into our UserControl code, we make sure that we already have Imports System.ComponentModel and Imports System.ComponentModel.Design references.

3. 在我们的 UserControl 类上,我们添加了一个 Designer 属性,以为此指定我们的 ControlDesigner用户控件.

3. Over our UserControl class, we add a Designer attribute, to specify our ControlDesigner for this UserControl.

Imports System.ComponentModel
Imports System.ComponentModel.Design

<Designer(GetType(MyControlDesigner))>
Public Class UserControl1
    'Our UserControl code in here...
End Class

4. 在我们的 UserControl 类下,我们创建一个名为MyControlDesigner"的新类,它将成为我们的 ControlDesigner.

4. Under our UserControl class, we create a new class by the name "MyControlDesigner" which will be our ControlDesigner.

Public Class MyControlDesigner

End Class

5. 现在,例如,让我们创建一个 Verb,它将 DockUndock 我们的 ParentForm 中的 >UserControl.

5. Now, for example, lets create a Verb which will Dock and Undock our UserControl in ParentForm.

Public Class MyControlDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner 'Inherit from ControlDesigner class.

    Private MyVerbs As DesignerVerbCollection

    Public Sub New()

    End Sub

    Public Overrides ReadOnly Property Verbs() As DesignerVerbCollection
        Get
            If MyVerbs Is Nothing Then
                MyVerbs = New DesignerVerbCollection 'A new DesignerVerbCollection to use for our DesignerVerbs.
                MyVerbs.Add(New DesignerVerb("Dock In ParentForm", New EventHandler(AddressOf OnMyCommandLinkClicked))) 'An Event Handler for Docking our UserControl.
                MyVerbs.Add(New DesignerVerb("Undock in ParentForm", New EventHandler(AddressOf OnMyCommandLinkClicked))) 'An Event Handler for Undocking our UserControl.
                MyVerbs(1).Visible = False 'We hide second Verd by default.
            End If
            Return MyVerbs
        End Get
    End Property

    Private Sub OnMyCommandLinkClicked(ByVal sender As Object, ByVal args As EventArgs)
        Dim _UserControl As UserControl1 = CType(Me.Control, UserControl1) 'Reference to our UserControl1 Class, so we can access it's Properties and Methods.
        If _UserControl.Dock = DockStyle.None Then 'If UserControl is Undocked then...
            _UserControl.Dock = DockStyle.Fill 'Dock UserControl in ParentForm.
            MyVerbs(0).Visible = False 'Hide "Dock In ParentForm" DesignerVerb.
            MyVerbs(1).Visible = True 'Show "Undock in ParentForm" DesignerVerb.
        Else
            _UserControl.Dock = DockStyle.None 'Undock UserControl.
            MyVerbs(1).Visible = False 'Hide "Undock in ParentForm" DesignerVerb.
            MyVerbs(0).Visible = True 'Show "Dock in ParentForm" DesignerVerb.
        End If
    End Sub
End Class

6.然后我们构建我们的项目并将我们的UserControl添加到我们的测试表单中.

6. Then we Build our project and we add our UserControl into our test Form.

这篇关于在 UserControl 的属性上添加 DesignerVerb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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