Panel not getting focus

后端 未结 6 2271
小鲜肉
小鲜肉 2020-11-22 14:42

I am continuing to program some kind of keyboard navigation in my simple graphic program (using C#). And I ran into trouble once again.

6条回答
  •  鱼传尺愫
    2020-11-22 14:51

    The code from Hans Passant translated to VB.NET

    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    
    Public Class SelectablePanel
        Inherits Panel
    
        Public Sub New()
            Me.SetStyle(ControlStyles.Selectable, True)
            Me.TabStop = True
        End Sub
        
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            Me.Focus()
            MyBase.OnMouseDown(e)
        End Sub
    
        Protected Overrides Function IsInputKey(ByVal keydata As Keys) As Boolean
            If (keydata = Keys.Up OrElse keydata = Keys.Down) Then Return True
            If (keydata = Keys.Left OrElse keydata = Keys.Right) Then Return True
            Return MyBase.IsInputKey(keydata)
        End Function
    
        Protected Overrides Sub OnEnter(ByVal e As EventArgs)
            Me.Invalidate()
            MyBase.OnEnter(e)
        End Sub
    
        Protected Overrides Sub OnLeave(ByVal e As EventArgs)
            Me.Invalidate()
            MyBase.OnLeave(e)
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
            MyBase.OnPaint(pe)
            If (Me.Focused) Then
                Dim rc As Rectangle = Me.ClientRectangle
                rc.Inflate(-2, -2)
                ControlPaint.DrawFocusRectangle(pe.Graphics, rc)
            End If
        End Sub
    
    End Class
    

提交回复
热议问题