Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Combobox search text that contains

Status
Not open for further replies.

daveonion

Programmer
Aug 21, 2002
359
GB
Hi,

Does anyone know how to do a contains search on the the windows combobox as opposed to only retrieving those that start with the characters i.e. the auto append property. For instance currently if you type ap then it would return apple as it begins with ap but it wouldnt retrieve sap because there is an S before the ap.

Hope this makes sense and would welcome any assistance


thanks
 
Perhaps something like this:

Code:
Public Class Form1

  Private AllItems As New List(Of String)
  Private SubItems As New List(Of String)
  Private Typing As Boolean = False

  Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

    SubItems.Clear()

    For a As Integer = 0 To AllItems.Count - 1
      If AllItems(a).ToLower.Contains(ComboBox1.Text.ToLower) Then SubItems.Add(AllItems(a))
    Next
    Typing = False

  End Sub

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    AllItems.Add("Fred")
    AllItems.Add("Theodore")
    AllItems.Add("Freda")
    AllItems.Add("John")
    AllItems.Add("Richard")
    AllItems.Add("Peter")
    AllItems.Add("Arthur")
    AllItems.Add("Samantha")

  End Sub

  Private Sub ComboBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Enter

    SubItems.Clear()

  End Sub

  Private Sub ComboBox1_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

    If SubItems.Count > 0 Then
      ComboBox1.Items.Clear()
      For a As Integer = 0 To SubItems.Count - 1
        ComboBox1.Items.Add(SubItems(a))
      Next
    Else
      ComboBox1.Items.Clear()
      For a As Integer = 0 To AllItems.Count - 1
        ComboBox1.Items.Add(AllItems(a))
      Next
    End If

  End Sub

  Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

    Typing = True

  End Sub
End Class

To use:

Run the program, click the down arrow - all names show. Close the list.

Type (for example) th and show the list - only the names with "th" in them show.
 
I've made a slight change to the TextChanged handler to allow for the DropDown to be visible while the user is typing.

Unfortuantely I've had to use SendKeys because I couldn't get the cursor to correctly position itself otherwise.

Code:
  Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

    SubItems.Clear()

    For a As Integer = 0 To AllItems.Count - 1
      If AllItems(a).ToLower.Contains(ComboBox1.Text.ToLower) Then SubItems.Add(AllItems(a))
    Next
    Typing = False
[bold]    If ComboBox1.DroppedDown Then
      My.Computer.Keyboard.SendKeys("{END}")
'the next line ensures that the refreshed DropDown is displayed by calling the DropDown handler
      ComboBox1_DropDown(ComboBox1, e)
    End If
[/bold]
  End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top