Search code examples
wpfvb.netcomboboxattached-properties

Deselecting text in ComboBox


I have a ComboBox with IsEditable is set to True so I'm able to type in the TextBox in the ComboBox. Now, I want the DropDownList to open so I created a Attached Property for it:

Public Shared Function GetShowDropDown(obj As DependencyObject) As Boolean
    Return obj.GetValue(IsDigitOnlyProperty)
End Function

Public Shared Sub SetShowDropDown(obj As DependencyObject, value As Boolean)
    obj.SetValue(IsDigitOnlyProperty, value)
End Sub

Public Shared ReadOnly ShowDropDownProperty As DependencyProperty = DependencyProperty.RegisterAttached("ShowDropDown", GetType(Boolean), GetType(ControlBehaviour), New UIPropertyMetadata(False, AddressOf OnShowDropDown))

Private Shared Sub OnShowDropDown(sender As Object, e As DependencyPropertyChangedEventArgs)
    Dim comboBox As ComboBox = sender
    Dim showDropDown As Boolean = e.NewValue

    If showDropDown Then
        AddHandler comboBox.PreviewKeyUp, AddressOf DoShowDropDown
    Else
        RemoveHandler comboBox.PreviewKeyUp, AddressOf DoShowDropDown
    End If
End Sub

Private Shared Sub DoShowDropDown(sender As Object, e As KeyEventArgs)
    Dim comboBox As ComboBox = sender
    If comboBox.Text.Length > 0 Then
        comboBox.IsDropDownOpen = True
    Else
        comboBox.IsDropDownOpen = False
    End If
End Sub

Whenever I type in the ComboBox, the DropDownList opens but there's 1 flaw. Let me explain it like this:

  • There are several items (Strings) in the list, 1 of them is "Test".
  • If I type the "T" in the TextBox of the ComboBox, "est" is added behind the "T" to show "Test".
  • Now "Test" is shown in the TextBox AND it is selected. So when I want to type the "e", "Test" is removed and only "e" shows up in the TextBox (normal behavior when you type if something is selected.

I don't want the last bullet to happen, if I type in the TextBox, nothing may be selected so I can keep typing "Test". Here is a screenshot from when I pressed the letter "T". I hope this explains my problem.

Screen when "T" is pressed

What should happen I think is that I have to deselect the test in some way or set it so the selection starts from the 2nd character in the TextBox but I have no clue how to do this.


To add something else with this, when I press Enter I want it to hide the DropDown.


Solution

  • Explanation

    We will do it using SelectedIndex property. We will select the -1 Index. As it is understood, there can't be any item as -1, so no item will be selected. The numbering or list of the items, whatever you can say, starts from 0.

    But, suppose you have 5 items, you can't give it the number 6, it will show a run time error.

    And for your additional help, you don't want that everything should go away and just e should come, right? For this, you can save all the text of combobox in a variable or textbox and then bring it back to the combobox with the text you entered later.

    We will do the second requirement in a timer event by setting the interval to 1 mili second, so that it can record the change more frequently. You can do this in any event you want (TextChanged etc.)

    Code And Example

    combobox.SelectedIndex = -1
    

    And for the text not to replace the existing one:

    Private Sub Form1_Load () Handles Mybase.Load
           Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Tick () Handles Timer1.Tick
           Textbox1.Text = Combobox.Text
    End Sub
    

    And assigning the text now being typed as another variable, we will write the following code:

    Combobox.Text = Textbox1.Text & a 'assuming 'a' is the text you entered later.
    

    I hope it will help you and work perfectly!