[英]Handling Focus with Custom Controls in VB.net
我有一個正在創建的自定義控件。 當我單擊它時,它會繪制一個虛線邊框,並在其上放一些小塊以調整大小。 所有這一切都完美。 現在我想要它,所以當我單擊它時,它會取消選擇。 我已經有了一個變量來設置是否被選中,以及用來繪制/清除它的子控件。 我只需要能夠檢測何時選擇了其他選項或將其單擊。
我嘗試過的
我對此的第一個也是最好的解決方案是使用LostFocus
事件,但是,通過自定義控件顯然不會觸發它。 據我所知,經過一些研究,自定義控件沒有Focus事件,因為它們是自定義的並且可以更改(基本上,您必須自己實現focus事件)。
我的問題
是否有人解決焦點事件或解決自定義控件單擊問題的方法?
資料來源
這是我的控件的當前來源:
Imports System.Drawing.Drawing2D
Public Class wDOMElement
Inherits Control
Public selected As Boolean = False
Private mdown As Boolean = False
Private moffset As Point = Nothing
Private nubs As New List(Of PictureBox)
Private Sub wDOMElement_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
MsgBox("test <-- DOES NOT SHOW!")
End Sub
Private Sub wDesignEditor_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If mdown Then Return
mdown = True
moffset = MousePosition - Location
selectME()
End Sub
Private Sub selectME()
selected = True
updateDraw()
updateBorder()
For Each v As PictureBox In nubs
v.Show()
Next
End Sub
Private Sub unselectME()
selected = False
updateBorder()
updateDraw()
For Each v As PictureBox In nubs
v.Hide()
Next
End Sub
Private Sub updateBorder()
Using gfx As Graphics = Graphics.FromHwnd(Me.Handle)
gfx.Clear(BackColor)
If selected Then
Dim blackPen As New Pen(Brushes.Black)
blackPen.DashStyle = DashStyle.Dot
gfx.DrawRectangle(blackPen, 4, 4, Width - 9, Height - 9)
End If
End Using
End Sub
Private Sub updateDraw()
'Needs to be overriden
End Sub
Private Sub configureFirstSelection()
Using gfx As Graphics = Graphics.FromHwnd(Me.Handle)
Dim blackPen As New Pen(Brushes.Black)
blackPen.DashStyle = DashStyle.Dot
gfx.DrawRectangle(blackPen, 4, 4, Width - 9, Height - 9)
'Top Handle
placeHandle(My.Resources.Handle, CInt(Width / 2 - 3), CInt(0), New Point(0, 1), New Point(0, -1), Cursors.SizeNS, AnchorStyles.Top)
'Bottom Handle
placeHandle(My.Resources.Handle, CInt(Width / 2 - 3), CInt(Height - 7), New Point(0, 0), New Point(0, 1), Cursors.SizeNS, AnchorStyles.Bottom)
'Left Handle
placeHandle(My.Resources.Handle, CInt(0), CInt(Height / 2 - 3), New Point(1, 0), New Point(-1, 0), Cursors.SizeWE, AnchorStyles.Left)
'Right Handle
placeHandle(My.Resources.Handle, CInt(Width - 7), CInt(Height / 2 - 3), New Point(0, 0), New Point(1, 0), Cursors.SizeWE, AnchorStyles.Right)
'Top Left Handle
placeHandle(My.Resources.Handle, CInt(0), CInt(0), New Point(1, 1), New Point(-1, -1), Cursors.SizeNWSE, AnchorStyles.Top + AnchorStyles.Left)
'Top Right Handle
placeHandle(My.Resources.Handle, CInt(Width - 7), CInt(0), New Point(0, 1), New Point(1, -1), Cursors.SizeNESW, AnchorStyles.Top + AnchorStyles.Right)
'Bottom Left Handle
placeHandle(My.Resources.Handle, CInt(0), CInt(Height - 7), New Point(1, 0), New Point(-1, 1), Cursors.SizeNESW, AnchorStyles.Bottom + AnchorStyles.Left)
'Bottom Right Handle
placeHandle(My.Resources.Handle, CInt(Width - 7), CInt(Height - 7), New Point(0, 0), New Point(1, 1), Cursors.SizeNWSE, AnchorStyles.Bottom + AnchorStyles.Right)
End Using
End Sub
Private Sub placeHandle(ByVal pic As Image, ByVal x As Integer, ByVal y As Integer, ByVal mov As Point, ByVal siz As Point, ByVal cur As Cursor, ByVal ancr As AnchorStyles)
Dim nPB As New PictureBox
nPB.SizeMode = PictureBoxSizeMode.AutoSize
nPB.Image = pic
nPB.Location = New Point(x, y)
nPB.Cursor = cur
nPB.Visible = False
nPB.Anchor = ancr
nPB.Parent = Me
Dim md As Boolean = False
Dim lpos As Point = Nothing
Dim moveClock As New Timer
moveClock.Interval = 1
moveClock.Enabled = False
AddHandler nPB.MouseDown, Sub()
md = True
lpos = MousePosition
moveClock.Start()
End Sub
AddHandler moveClock.Tick, Sub()
If md Then
Dim nX As Integer = (MousePosition.X - lpos.X) * mov.X
Dim nY As Integer = (MousePosition.Y - lpos.Y) * mov.Y
Dim nWidth As Integer = (MousePosition.X - lpos.X) * siz.X
Dim nHeight As Integer = (MousePosition.Y - lpos.Y) * siz.Y
Left += nX
Top += nY
Width += nWidth
Height += nHeight
lpos = MousePosition
updateDraw()
updateBorder()
End If
End Sub
AddHandler nPB.MouseUp, Sub()
md = False
moveClock.Stop()
End Sub
nubs.Add(nPB)
End Sub
Private Sub wDesignEditor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If mdown Then
Location = MousePosition - moffset
End If
End Sub
Private Sub wDesignEditor_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
mdown = False
End Sub
End Class
嘗試對您的自定義控件使用Enter和Leave事件。
GotFocus和LostFocus已經過時,轉而支持Enter和Leave。
更新:
由於您是從Control繼承的,因此您可能應該覆蓋事件,而不是處理它們。 MouseDown也不一定會吸引焦點,因此您也應該檢查一下:
例:
Public Class ControlEx
Inherits Control
Protected Overrides Sub OnEnter(e As EventArgs)
MyBase.OnEnter(e)
MessageBox.Show("Hello")
End Sub
Protected Overrides Sub OnLeave(e As EventArgs)
MyBase.OnLeave(e)
MessageBox.Show("Good-bye")
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
If Me.Enabled AndAlso Not Me.Focused Then
Me.Focus()
End If
MyBase.OnMouseDown(e)
End Sub
End Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.