What is the difference between .Focus and .Select

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
In VB6, Something.SetFocus sets the focus to that Something. No problems there - in .Net it's called .Focus.

On another thread I was told (twice!) to use Something.Select in a particular scenario (which works fine by the way).

So, what's the difference, please? They both seem to do the same thing.
 
Technically, if I have a look at them in Reflector, they are doing the same thing.

Focus leads down to InternalFocus:
VB.NET:
Friend Overridable Function FocusInternal() As Boolean
    If Me.CanFocus Then
        UnsafeNativeMethods.SetFocus(New HandleRef(Me, Me.Handle))
    End If
    If (Me.Focused AndAlso (Not Me.ParentInternal Is Nothing)) Then
        Dim containerControlInternal As IContainerControl = Me.ParentInternal.GetContainerControlInternal
        If (Not containerControlInternal Is Nothing) Then
            If TypeOf containerControlInternal Is ContainerControl Then
                DirectCast(containerControlInternal, ContainerControl).SetActiveControlInternal(Me)
            Else
                containerControlInternal.ActiveControl = Me
            End If
        End If
    End If
    Return Me.Focused
End Function

And Select leads to Select(directed As Boolean, Byval forward as Boolean) (with both set to false, but they're not used anyway *cool*):
VB.NET:
Protected Overridable Sub [Select](ByVal directed As Boolean, ByVal forward As Boolean)
    Dim containerControlInternal As IContainerControl = Me.GetContainerControlInternal
    If (Not containerControlInternal Is Nothing) Then
        containerControlInternal.ActiveControl = Me
    End If
End Sub

So, Focus() is just doing some checks more if it is even possible to set the Focus to the control...as far as I see it.

Bobby
 
help said:
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.
It doesn't get any clearer than that, does it?

As for the internal code, SetFocus involves various windows messages the other methods don't use.
 
Back
Top