Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

   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

?

  • Leaders
Posted

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.

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.

[sIGPIC]e[/sIGPIC]
Posted
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.

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...