Search code examples
vb.netuser-controlsvisual-studio-2017

Add DesignerVerb on UserControl's Properties


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.

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?

enter image description here


Solution

  • Well, here is a simple example...

    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. Into our UserControl code, we make sure that we already have Imports System.ComponentModel and Imports System.ComponentModel.Design references.

    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. Under our UserControl class, we create a new class by the name "MyControlDesigner" which will be our ControlDesigner.

    Public Class MyControlDesigner
    
    End Class
    

    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. Then we Build our project and we add our UserControl into our test Form.

    enter image description here enter image description here