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.
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
For an example of how they are different, if you are trying to set a control for a Forms App to default focus to when you open it, only Select() will work when called in the constructor after InitializeComponent(). Focus() will not.
Just to add to this thread I found that when writing a user control that moved other controls from one form to another (newly created form). The original form could no longer select the control but using focus allowed it to do so. I think this emphasises the answers about the levels these methods work at. But it also means it is not simple enough to say use Select at the higher level since select no longer worked as expected on the orginal form (not that it should being I placed it into a different form - I accept that)
Focus(), in some situations, can cause a window owning the control to gain focus if it didn't have focus. Select() does not cause a focus grab by the window.
From personal experience I wrote a user control inheriting the Windows ComboBox. I had to write code to override the OnEnter
event and I had a statement in there saying
If Me.Focused Then ... Else ...
However, unfortunately it returned the unexpected result. If I called MyCustomerComboControl.Select
(in either Load, Shown or Activated events) it called the OnEnter
method but failed to register it had the focus (i.e. Focused was False) but if I called Focus it worked. Furthermore Select
worked if the form was open i.e. if I selected another control then re-selected the original control all was fine. So in any other circumstances other than my scenario, use Select
because it says so above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With