如何在设计模式下以编程方式选择控件

有人可以告诉我如何在设计模式下以编程方式选择控件。

我试图在VB.net中创建一个包含两个面板的自定义用户控件。我希望整个用户控件都可以在设计模式下轻松选择。我已包含以下示例并尝试了。我能够识别所选控件,但无法选择它。

我正在使用Visual Studio 2015 / Windows 10 Pro。

1.制作项目

新建项目-> Windows窗体应用程序->名称:示例-> [确定]

2.添加用户控件

添加->新建项目->用户控件->名称:usrPanel.vb-> [添加]

usrPanel.BorderStyle:固定的

3,在usrPanel上添加2个面板控件

在usrPanel上添加面板控件

名称:pnlTitle

pnlTitle.Dock:顶部

pnlTitle.BackColor:栗色

再添加一个面板

名称:pnlMain

pnlMain.Dock:完整

4.像这样修改usrPanel.vb

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.在下面粘贴整个示例。

https://docs.microsoft.com/zh-cn/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.Mouse Clidk之后,我想在设计模式下选择指定的面板

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

更改

            '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,全部构建

8,在Form1上添加usrPanel控件

usrPanel已显示

单击usrPanel的标题->已选择标题

单击蓝点->没有任何反应

单击usrPanel的右边缘->整个面板被选中

单击蓝点后,如何获取usrPanel?

锡锰

WinForm设计器代码全部与使用设计器服务有关。在这种情况下,您需要获取选择服务的实例,该实例通过实现ISelectionService接口的对象来体现

您可以通过实现IServiceProvider接口的对象获取服务实例Control.Site物业实现的ISite接口,反过来工具的IServiceProvider。

使用IServiceProvider.GetService(Type)方法获取服务,然后使用ISelectionService.SetSelectedComponents方法选择所需的组件。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jqGrid-如何在单元格编辑模式下以编程方式选择单元格?

来自分类Dev

如何在WPF中使用MVVM设计模式以编程方式选择和设置数据网格行的焦点

来自分类Dev

如何在Netbean的设计模式下更改JAVA SWING控件名称

来自分类Dev

如何在asp.net中的日历控件中以编程方式选择和取消选择多个日期?

来自分类Dev

如何在设计模式下调试WPF用户控件?

来自分类Dev

如何选择设计模式

来自分类Dev

如何在编辑模式下绑定下拉控件?

来自分类Dev

如何在WebView中以编程方式退出全屏模式?

来自分类Dev

如何在Android中以编程方式检查保持模式

来自分类Dev

如何在设计模式的帮助下使用继承

来自分类Dev

SSRS 如何在设计模式下隐藏参数/分组面板

来自分类Dev

如何在不选择任何内容的情况下以编程方式在Finder中打开文件夹?

来自分类Dev

如何在设计时在delphi中的自定义控件中选择子控件

来自分类Dev

如何在Windows下以编程方式清除回收站?

来自分类Dev

如何在Windows下以编程方式清除回收站?

来自分类Dev

如何以编程方式隐藏QtDesigner“设计”模式创建的小部件?

来自分类Dev

如何在Highcharts中以编程方式选择多个点或标记?

来自分类Dev

如何在活动中以编程方式选择RadioButton

来自分类Dev

如何在TBCEditor中以编程方式设置文本选择?

来自分类Dev

如何在PowerPoint中以编程方式进行多次选择

来自分类Dev

如何在Ag-Grid中以编程方式选择多行?

来自分类Dev

如何在TBCEditor中以编程方式设置文本选择?

来自分类Dev

如何在活动中以编程方式选择RadioButton

来自分类Dev

如何以编程方式添加用户控件

来自分类Dev

如何以编程方式删除窗体控件

来自分类Dev

如何在通用Windows上以编程方式设置控件的列或行跨度?

来自分类Dev

如何在不编辑XAML的情况下向设计器中的网格单元添加控件?

来自分类Dev

如何在 Delphi 中以编程方式设置 TDatamodule 的设计时高度/宽度?

来自分类Dev

如何在asp.net C#的“编辑”模式下替换“未选择文件”中的文件上传控件中已保存的文件?

Related 相关文章

  1. 1

    jqGrid-如何在单元格编辑模式下以编程方式选择单元格?

  2. 2

    如何在WPF中使用MVVM设计模式以编程方式选择和设置数据网格行的焦点

  3. 3

    如何在Netbean的设计模式下更改JAVA SWING控件名称

  4. 4

    如何在asp.net中的日历控件中以编程方式选择和取消选择多个日期?

  5. 5

    如何在设计模式下调试WPF用户控件?

  6. 6

    如何选择设计模式

  7. 7

    如何在编辑模式下绑定下拉控件?

  8. 8

    如何在WebView中以编程方式退出全屏模式?

  9. 9

    如何在Android中以编程方式检查保持模式

  10. 10

    如何在设计模式的帮助下使用继承

  11. 11

    SSRS 如何在设计模式下隐藏参数/分组面板

  12. 12

    如何在不选择任何内容的情况下以编程方式在Finder中打开文件夹?

  13. 13

    如何在设计时在delphi中的自定义控件中选择子控件

  14. 14

    如何在Windows下以编程方式清除回收站?

  15. 15

    如何在Windows下以编程方式清除回收站?

  16. 16

    如何以编程方式隐藏QtDesigner“设计”模式创建的小部件?

  17. 17

    如何在Highcharts中以编程方式选择多个点或标记?

  18. 18

    如何在活动中以编程方式选择RadioButton

  19. 19

    如何在TBCEditor中以编程方式设置文本选择?

  20. 20

    如何在PowerPoint中以编程方式进行多次选择

  21. 21

    如何在Ag-Grid中以编程方式选择多行?

  22. 22

    如何在TBCEditor中以编程方式设置文本选择?

  23. 23

    如何在活动中以编程方式选择RadioButton

  24. 24

    如何以编程方式添加用户控件

  25. 25

    如何以编程方式删除窗体控件

  26. 26

    如何在通用Windows上以编程方式设置控件的列或行跨度?

  27. 27

    如何在不编辑XAML的情况下向设计器中的网格单元添加控件?

  28. 28

    如何在 Delphi 中以编程方式设置 TDatamodule 的设计时高度/宽度?

  29. 29

    如何在asp.net C#的“编辑”模式下替换“未选择文件”中的文件上传控件中已保存的文件?

热门标签

归档