Setting Focus to a .NET UserControl…?

后端 未结 7 717
不思量自难忘°
不思量自难忘° 2020-12-18 23:16

I\'m creating a custom control derived from UserControl that I would like to set focus to.

The custom control contains a ComboBox control and I draw some strings bes

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 00:08

    Say, you have a picture on your user control and you want to highlight it mimicking the "GetFocus" event (say focus on your user control takes this picture). The focus on your user control will be handled by drawing an outline dashed line to the PictureBox. This is done through your user control OnEnter and OnLeave events. Here is the highlight procedure...

    Public Sub highlightImage()
        Dim l As Single() = {2, 2, 2, 2}
        Dim p As New Pen(Color.Gray, 1)
        p.DashPattern = l
        Dim g As Graphics = picColor.CreateGraphics()
        g.DrawRectangle(p, 0, 0, picColor.Width - 1, picColor.Height - 1)
    End Sub
    

    These two overrides will do the job.

    Protected Overrides Sub OnEnter(e As EventArgs)
        MyBase.OnEnter(e)
        Me.highlightImage()
    End Sub
    Protected Overrides Sub OnLeave(e As EventArgs)
        MyBase.OnLeave(e)
        MyBase.Refresh()
    End Sub
    

提交回复
热议问题