In WinForms, to set focus to a specific control, I always seem to wind up calling Control.Select()
and Control.Focus()
to get it to work.
What is the difference, and is this the correct approach?
In WinForms, to set focus to a specific control, I always seem to wind up calling Control.Select()
and Control.Focus()
to get it to work.
What is the difference, and is this the correct approach?
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
Focus() is the low level function that actually sets the focus.
Select() is a higer-level method. It first looks iteratively upward in the control's parent hierarchy until it finds a container control. Then it sets that container's ActiveControl property (to the called control). The logic in those methods is not straightforward however, and there is special handling for UserControl containers.