Combobox Text Selection

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Why does the combobox select all text when it gets focus? I don't see any need for this and note that textboxes don't do it. And is there anything better that I can do to fix itthan:

Visual Basic:
    Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus
        Me.ComboBox1.Select(Me.ComboBox1.Text.Length, 0)
    End Sub
?
 
I wish there was an option for the textbox to do that. I think you have the right idea with your solution, though. My only recommendation is that you might want to subclass the combo box to make this solution easier to implement.
Visual Basic:
Public Class BetterComboBox
    Inherits ComboBox

    'Track the selection when the user leaves the combo box    
    Dim LastSelection As Integer
    Public Overrides Sub OnLostFocus(e As EventArgs)
        LastSelection = SelectionStart
    End Sub
 
    'If the option is enabled, use the same selection
    'that was had when focus was lost
    Public Overrides Sub OnGotFocus(e As EventArgs)
        If _DontSelectEverything Then
            Select(LastSelection, 0)
        End If
    End Sub
    
    'Present the developer with an option to override
    'default selection on focus
    Private _DontSelectEverything As Boolean
    Public Property DontSelectEverything As Boolean
        Get
            Return _DontSelectEverything
        End Get
        Set(Value As Boolean)
            _DontSelectEverything = Value
        End Set
    End Property
End Class
I didn't test the code, but it should give you the idea.
 
marble_eater said:
I wish there was an option for the textbox to do that. I think you have the right idea with your solution, though. My only recommendation is that you might want to subclass the combo box to make this solution easier to implement.
Visual Basic:
Public Class BetterComboBox
    Inherits ComboBox

    'Track the selection when the user leaves the combo box    
    Dim LastSelection As Integer
    Public Overrides Sub OnLostFocus(e As EventArgs)
        LastSelection = SelectionStart
    End Sub
 
    'If the option is enabled, use the same selection
    'that was had when focus was lost
    Public Overrides Sub OnGotFocus(e As EventArgs)
        If _DontSelectEverything Then
            Select(LastSelection, 0)
        End If
    End Sub
    
    'Present the developer with an option to override
    'default selection on focus
    Private _DontSelectEverything As Boolean
    Public Property DontSelectEverything As Boolean
        Get
            Return _DontSelectEverything
        End Get
        Set(Value As Boolean)
            _DontSelectEverything = Value
        End Set
    End Property
End Class
I didn't test the code, but it should give you the idea.
Yes, I get the idea. Thing is, the user is more likely to select the combobox by clicking with the mouse than using the Tab key. What he really wants then is for the caret to appear at the point between characters where he clicked. I suppose I could manage that with MeasureString.
 
Back
Top