Search code examples
vb.netwinformsuser-controlsglyphdesignmode

How to select a control programmatically in design mode


Could someone please tell me how to select a control programmatically in design mode.

I tried to create a custom user control in VB.net that consists of two panels. I want the whole user control to be easily selectable in design mode. I have included the following sample and tried. I was able to identify the selected control but can't SELECT it.

I am using visual studio 2015 / windows 10 Pro.

1.Make Project

New Project -> Windows Forms application -> name : sample -> [OK]

2.Add User Control

Add -> New Item -> User Control -> Name : usrPanel.vb -> [Add]

usrPanel.BorderStyle : FixedSingle

3.Add 2 Panel controls on usrPanel

Add Panel control on usrPanel

name : pnlTitle

pnlTitle.Dock : Top

pnlTitle.BackColor : Maroon

Add one more Panel

name : pnlMain

pnlMain.Dock : Full

4.Modify usrPanel.vb like this

Imports System.ComponentModel
Imports System.Windows.Forms.Design
Imports System.Windows.Forms.Design.Behavior

<Designer(GetType(usrPanelDesigner))>
Public Class usrPanel

End Class
Public Class usrPanelDesigner
    Inherits ParentControlDesigner

    Private myAdorner As Adorner

    Public Overrides Sub Initialize(component As IComponent)

        MyBase.Initialize(component)
        If (TypeOf MyBase.Control Is usrPanel) Then
            MyBase.EnableDesignMode(DirectCast(MyBase.Control, usrPanel).pnlTitle, "Title")
            MyBase.EnableDesignMode(DirectCast(MyBase.Control, usrPanel).pnlMain, "Main")
        End If

        myAdorner = New Adorner()
        BehaviorService.Adorners.Add(myAdorner)
        myAdorner.Glyphs.Add(New MyGlyph(BehaviorService, Control))
    End Sub

End Class

5.Paste this entire sample below.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.design.behavior.behavior?redirectedfrom=MSDN&view=netcore-3.1

Class MyGlyph
    Inherits Glyph
    Private control As Control
    Private behaviorSvc As _
        System.Windows.Forms.Design.Behavior.BehaviorService

    Public Sub New(ByVal behaviorSvc As _
        System.Windows.Forms.Design.Behavior.BehaviorService, _
        ByVal control As Control)

        MyBase.New(New MyBehavior())
        Me.behaviorSvc = behaviorSvc
        Me.control = control
    End Sub

    Public Overrides ReadOnly Property Bounds() As Rectangle
        Get
            ' Create a glyph that is 10x10 and sitting
            ' in the middle of the control.  Glyph coordinates
            ' are in adorner window coordinates, so we must map
            ' using the behavior service.
            Dim edge As Point = behaviorSvc.ControlToAdornerWindow(control)
            Dim size As Size = control.Size
            Dim center As New Point(edge.X + size.Width / 2, edge.Y + _
                size.Height / 2)

            Dim bounds1 As New Rectangle(center.X - 5, center.Y - 5, 10, 10)

            Return bounds1
        End Get
    End Property

    Public Overrides Function GetHitTest(ByVal p As Point) As Cursor
        ' GetHitTest is called to see if the point is
        ' within this glyph.  This gives us a chance to decide
        ' what cursor to show.  Returning null from here means
        ' the mouse pointer is not currently inside of the glyph.
        ' Returning a valid cursor here indicates the pointer is
        ' inside the glyph,and also enables our Behavior property
        ' as the active behavior.
        If Bounds.Contains(p) Then
            Return Cursors.Hand
        End If

        Return Nothing

    End Function


    Public Overrides Sub Paint(ByVal pe As PaintEventArgs)
        ' Draw our glyph.  It is simply a blue ellipse.
        pe.Graphics.FillEllipse(Brushes.Blue, Bounds)

    End Sub

    ' By providing our own behavior we can do something interesting
    ' when the user clicks or manipulates our glyph.

    Class MyBehavior
        Inherits System.Windows.Forms.Design.Behavior.Behavior

        Public Overrides Function OnMouseUp(ByVal g As Glyph, _
            ByVal button As MouseButtons) As Boolean
            MessageBox.Show("Hey, you clicked the mouse here")
            Return True
            ' indicating we processed this event.
        End Function 'OnMouseUp
    End Class

End Class

6.After Mouse Clidk I want to select the specified panel in Design Mode

            MessageBox.Show("Hey, you clicked the mouse here")

Change

            'MessageBox.Show("Hey, you clicked the mouse here")
            Dim myg As MyGlyph = CType(g, MyGlyph)
            Dim c As Control = myg.control
            c.Select()
            c.Focus()

7.Build All

8.Add usrPanel control on Form1

usrPanel was displayed

click header of usrPanel -> header SELECTED

click blue point -> nothing happens

click the right edge of the usrPanel -> The entire panel is SELECTED

How do I get the usrPanel to be SELECTED when the blue point is clicked?


Solution

  • WinForm Designer code is all about using designer services. In this case you need to acquire an instance of the selection service that is manifested through an object that implements the ISelectionService Interface.

    You acquire a service instance through an object that implements the IServiceProvider Interface. The Control.Site Property implements the ISite Interface that in turn implements IServiceProvider.

    Use the IServiceProvider.GetService(Type) Method to acquire the service and then use the ISelectionService.SetSelectedComponents Method to select the desired component(s).

    Public Overrides Function OnMouseUp(g As Glyph, button As MouseButtons) As Boolean
      Dim myg As MyGlyph = CType(g, MyGlyph)
      Dim c As Control = myg.control
      Dim selService As ISelectionService = CType(c.Site.GetService(GetType(ISelectionService)), ISelectionService)
      selService?.SetSelectedComponents({c})
      Return True
    End Function