Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Combo Box Filter

Combo Box Filter

Scheduled Pinned Locked Moved Visual Basic
question
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lildragon
    wrote on last edited by
    #1

    Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

    C R S L 5 Replies Last reply
    0
    • L lildragon

      Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

      C Offline
      C Offline
      ccotton333
      wrote on last edited by
      #2

      Are you pulling the item that you fill the combo box with from an access database?

      L 1 Reply Last reply
      0
      • C ccotton333

        Are you pulling the item that you fill the combo box with from an access database?

        L Offline
        L Offline
        lildragon
        wrote on last edited by
        #3

        At the moment I am not, but I have created an arraylist to populate the data source. But after the database is completed I will be using access to populate the combo box.

        1 Reply Last reply
        0
        • L lildragon

          Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

          R Offline
          R Offline
          Ravi S V
          wrote on last edited by
          #4

          Actually, I am new VB.Net instead of putting all the items in a collection, put the items in an array, so that, when ever u need an item from that array u can use filter function which finds the selected item and returns an zero-based array. Thanx, Ravi.

          A 1 Reply Last reply
          0
          • L lildragon

            Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

            S Offline
            S Offline
            Serge Lobko Lobanovsky
            wrote on last edited by
            #5

            Hi, How about creating a custom class that inherits from ArrayList and provides a readonly property which returns a filtered list? Like this:

            Public Class MyItem
            Public Field1 As String
            End Class

            Public Class MyList
            Inherits ArrayList

            Public Readolny Property FilteredByField1(FilterFieldVal As String) As MyList
            Get
            Dim mi As MyItem
            Dim rcol As New MyList

               For Each mi In Me
                 If mi.Field1 = FilterFieldVal Then rcol.Add(mi)
               Next
            
               Return rcol
             End Get
            

            End Property
            End Class

            Regards, Serge (Logic Software, Easy Projects .NET site)

            A 1 Reply Last reply
            0
            • R Ravi S V

              Actually, I am new VB.Net instead of putting all the items in a collection, put the items in an array, so that, when ever u need an item from that array u can use filter function which finds the selected item and returns an zero-based array. Thanx, Ravi.

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Hi, thanks for your advice, I'll try that idea out. I was just wondering how it would work when I upgrade from a manually set of data to a dataset from a data base? thanks.

              1 Reply Last reply
              0
              • S Serge Lobko Lobanovsky

                Hi, How about creating a custom class that inherits from ArrayList and provides a readonly property which returns a filtered list? Like this:

                Public Class MyItem
                Public Field1 As String
                End Class

                Public Class MyList
                Inherits ArrayList

                Public Readolny Property FilteredByField1(FilterFieldVal As String) As MyList
                Get
                Dim mi As MyItem
                Dim rcol As New MyList

                   For Each mi In Me
                     If mi.Field1 = FilterFieldVal Then rcol.Add(mi)
                   Next
                
                   Return rcol
                 End Get
                

                End Property
                End Class

                Regards, Serge (Logic Software, Easy Projects .NET site)

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                Hi, thanks for the advice. I was looking at using an array list to store the original data from the combobox and using that data to repopulate the combobox on the leave event. I will try this out and see how it works out. I have never used the filter functions of arraylists. Cheers

                1 Reply Last reply
                0
                • L lildragon

                  Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

                  L Offline
                  L Offline
                  lildragon
                  wrote on last edited by
                  #8

                  Hi everyone! Thanks for all of your help. I am now stuck on a key validation problem. My backspace does not delete enough of the text within a combo box for me to reset the combobox and append text etc. It will only delete this text if I hold down the backspace button. Does anyone have any suggestions? Test out the code and see if you can find anyother bugs. I also would like for the text not to completely fill the box if it is the last item until the user selects the item from the list or leaves focus on the box(would like to keep it highlighted just as if there were other items in the list). Thanks everyone!! Imports System.Drawing.Graphics Imports System.Drawing.Brush Imports System.Drawing.Drawing2D Imports System.Drawing Imports System.ComponentModel.Design.Serialization Imports System.ComponentModel Public Class Form1 Inherits System.Windows.Forms.Form Dim origData_cbo As New ArrayList #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call origData_cbo.Add("Please Select or Type an Item Name") origData_cbo.Add("acada") origData_cbo.Add("acura") origData_cbo.Add("amy") origData_cbo.Add("apple") origData_cbo.Add("bananna") origData_cbo.Add("orange") origData_cbo.Add("pear") origData_cbo.Add("person") origData_cbo.Add("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") cbo1.DataSource = origData_cbo End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Uc_measurements2 As SingerProtype.uc_measurements Friend WithEvents Uc_measurements3 As SingerProtype.uc_measurements Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePi

                  1 Reply Last reply
                  0
                  • L lildragon

                    Hi I am trying to create a combobox that will filter out items found in an items collection as the user enters each character. This combo box will also need to display the items within the items collection as well. I have found code that will allow me to autofill the combo box and to display the items in the collection, but have not had any luck in coding or finding code that will allow me to filter out these items. Any suggestions? Thanks. The code found so far: Public Sub AutoCompleteCombo_keyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Dim cbo As ComboBox = sender Dim sTypedText As String Dim iFoundIndex As Integer Dim oFoundItem As Object Dim sFoundText As String Dim sAppendText As String Dim boxIndex As Integer, lExst As Boolean Dim posCursor As Integer = cbo.SelectionStart ' If Cursor does not stay on the beginning of text box. If posCursor <> 0 Then lExst = False 'Allow select keys without Autocompleting Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down Return End Select If e.KeyCode = Keys.Enter Then cbo.Height = 21 'RaiseEvent Submit(Me, e.Empty) Exit Sub End If 'Get the Typed Text and Find it in the list sTypedText = cbo.Text iFoundIndex = cbo.FindString(sTypedText) 'If we found the Typed Text in the list then Autocomplete If iFoundIndex >= 0 Then If cbo.Text <> "" Then 'cbo.DataSource().GetType.mDataViewCombo.RowFilter = cbo.DisplayMember & " LIKE '" & Me.Text & "%'" Else 'mDataViewCombo.RowFilter = "" End If 'Get the Item from the list (Return Type depends if Datasource was bound ' or List Created) oFoundItem = cbo.Items(iFoundIndex) 'Use the ListControl.GetItemText to resolve the Name in case the Combo ' was Data bound sFoundText = cbo.GetItemText(oFoundItem) 'Append then found text to the typed text to preserve case sAppendText = sFoundText.Substring(sTypedText.Length) cbo.Text = sTypedText & sAppendText 'Select the Appended Text cbo.Selection

                    L Offline
                    L Offline
                    lildragon
                    wrote on last edited by
                    #9

                    Hi everyone, this code handles backspaces, populates the combobox..filters out the stuff that doesn't match the users input, but I am having a problem when I try to type strings that match a single word, but with more chars. I would like to be able to continue typing and displaying nothing in the combobox type thing. But, when I type the extra chars, the combobox forces me back to the start position of the combobox. Does anyone have any idea what is going on? Thank you. Imports System.Drawing.Graphics Imports System.Drawing.Brush Imports System.Drawing.Drawing2D Imports System.Drawing Imports System.ComponentModel.Design.Serialization Imports System.ComponentModel Public Class Form1 Inherits System.Windows.Forms.Form Dim origData_cbo As New ArrayList #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call origData_cbo.Add("Please Select or Type an Item Name") origData_cbo.Add("acada") origData_cbo.Add("acura") origData_cbo.Add("amy") origData_cbo.Add("apple") origData_cbo.Add("bananna") origData_cbo.Add("orange") origData_cbo.Add("pear") origData_cbo.Add("person") origData_cbo.Add("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") cbo1.DataSource = origData_cbo End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Uc_measurements2 As SingerProtype.uc_measurements Friend WithEvents Uc_measurements3 As SingerProtype.uc_measurements Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker Friend WithEvents Uc_measurements1 As SingerProtype.uc_measurements Friend WithEvents cbo1 As System.Windows.Forms.ComboBox F

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups