Setting Focus to a .NET UserControl…?

后端 未结 7 688
不思量自难忘°
不思量自难忘° 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条回答
  • 2020-12-19 00:16

    UserControl will fight you tooth and nail to avoid getting the focus. It has code that automatically passes the focus to a child control (if any) if it does get the focus. You'll at a minimum have to override WndProc() and trap the WM_SETFOCUS message. There might be other surgery needed, like ControlStyles.Selectable and the TabStop and TabIndex properties.

    Your next issue is that UserControl won't respond meaningfully to, say, keyboard messages when it does have focus. You'll need to detect clicks on the UC background to handle mouse messages, as well as override the painting so it is obvious to the user that the UC has the focus (use ControlPaint.DrawFocusRectangle). If this starts to sound unattractive, it's because UC was really meant to be a container control.

    0 讨论(0)
提交回复
热议问题