Подскажите, пожалуйста, как программно выбрать элемент управления в режиме разработки.
Я попытался создать настраиваемый пользовательский элемент управления в VB.net, состоящий из двух панелей. Я хочу, чтобы весь пользовательский элемент управления можно было легко выбирать в режиме разработки. Я включил следующий образец и попробовал. Я смог идентифицировать выбранный элемент управления, но не могу его ВЫБРАТЬ.
Я использую Visual Studio 2015 / Windows 10 Pro.
1.Создать проект
Новый проект - ›Приложение Windows Forms -› name: sample - ›[OK]
2.Добавить пользовательский контроль
Добавить - ›Новый элемент -› Управление пользователем - ›Имя: usrPanel.vb -› [Добавить]
usrPanel.BorderStyle: FixedSingle
3. Добавьте 2 элемента управления Panel на usrPanel
Добавить элемент управления Panel на 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. Вставьте весь образец ниже.
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. После нажатия кнопки мыши я хочу выбрать указанную панель в режиме дизайна.
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. Добавьте элемент управления usrPanel в форму Form1.
usrPanel был отображен
щелкните заголовок usrPanel - ›ВЫБРАН заголовок
нажмите синюю точку - ›ничего не происходит
щелкните правый край usrPanel - ›ВЫБРАНА вся панель
Как получить ВЫБРАТЬ usrPanel при щелчке по синей точке?