views:

566

answers:

2

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?

+10  A: 

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

Daniel A. White
I don't think this is a good enough answer. I read that and understand that. I wrote a custom control. I don't know when it's appropriate for me to use Select vs Focus in my custom control, however. Is it an always thing? Is there some set of criteria? what are the implications of one versus the other, both actual and semantic?
Greg D
+8  A: 

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.