如何在设计模式下以编程方式 select 控件
How to select a control programmatically in design mode
有人可以告诉我如何在设计模式下以编程方式select控件。
我试图在 VB.net 中创建一个包含两个面板的自定义用户控件。
我希望整个用户控件能够在设计模式下轻松 select。
我已经包含了以下示例并进行了尝试。
我能够识别 selected 控件,但不能 SELECT 它。
我正在使用 visual studio 2015 / windows 10 Pro.
1.Make 项目
新建项目 -> Windows 表单申请
-> 名称:示例 -> [确定]
2.Add 用户控制
添加 -> 新项目 -> 用户控制 -> 名称:usrPanel.vb -> [添加]
usrPanel.BorderStyle : 固定单
3.Add usrPanel 上的 2 个面板控件
在 usrPanel 上添加面板控件
姓名 : pnlTitle
pnlTitle.Dock : 前
pnlTitle.BackColor : 栗色
再添加一个面板
名称:pnlMain
pnlMain.Dock : 全
4.Modify 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.Paste 下面是整个示例。
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鼠标点击我要select指定面板
在设计模式中
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.Build全部
8.Add Form1 上的 usrPanel 控件
已显示 usrPanel
单击 usrPanel 的 header -> header SELECTED
点击蓝点 -> 没有任何反应
单击 usrPanel 的右边缘 -> 整个面板 SELECTED
单击蓝点时如何让 usrPanel 变为 SELECTED?
WinForm Designer 代码都是关于使用设计器服务的。在这种情况下,您需要获取 selection 服务的实例,该实例通过实现 ISelectionService Interface.
的对象显示
您通过实现 IServiceProvider Interface. The Control.Site Property implements the ISite Interface 的对象获取服务实例,后者又实现 IServiceProvider。
使用 IServiceProvider.GetService(Type) Method to acquire the service and then use the ISelectionService.SetSelectedComponents Method 到 select 所需的组件。
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
有人可以告诉我如何在设计模式下以编程方式select控件。
我试图在 VB.net 中创建一个包含两个面板的自定义用户控件。 我希望整个用户控件能够在设计模式下轻松 select。 我已经包含了以下示例并进行了尝试。 我能够识别 selected 控件,但不能 SELECT 它。
我正在使用 visual studio 2015 / windows 10 Pro.
1.Make 项目
新建项目 -> Windows 表单申请 -> 名称:示例 -> [确定]
2.Add 用户控制
添加 -> 新项目 -> 用户控制 -> 名称:usrPanel.vb -> [添加]
usrPanel.BorderStyle : 固定单
3.Add usrPanel 上的 2 个面板控件
在 usrPanel 上添加面板控件
姓名 : pnlTitle
pnlTitle.Dock : 前
pnlTitle.BackColor : 栗色
再添加一个面板
名称:pnlMain
pnlMain.Dock : 全
4.Modify 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.Paste 下面是整个示例。
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鼠标点击我要select指定面板 在设计模式中
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.Build全部
8.Add Form1 上的 usrPanel 控件
已显示 usrPanel
单击 usrPanel 的 header -> header SELECTED
点击蓝点 -> 没有任何反应
单击 usrPanel 的右边缘 -> 整个面板 SELECTED
单击蓝点时如何让 usrPanel 变为 SELECTED?
WinForm Designer 代码都是关于使用设计器服务的。在这种情况下,您需要获取 selection 服务的实例,该实例通过实现 ISelectionService Interface.
的对象显示您通过实现 IServiceProvider Interface. The Control.Site Property implements the ISite Interface 的对象获取服务实例,后者又实现 IServiceProvider。
使用 IServiceProvider.GetService(Type) Method to acquire the service and then use the ISelectionService.SetSelectedComponents Method 到 select 所需的组件。
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