How do you create a two column combobox with a hidden column of values and a visible column of names.

Cynolycus 285 Reputation points
2023-10-12T13:11:09.54+00:00

I am trying to create some filters to display the matching results from a table.

I am not using a traditional database, all data is from a text file.

Currently I have multiple tables that the user can choose from but all tables contain the same columns, for example "Category".

When the user selects which table they want, that table is loaded, a unique list of categories is created in a list of strings which is then sorted and added to a combobox.

Before the list is added to the combobox an entry is added.

CB_Categories.Items.Add("")
For i = 0 To StrCategories.Count - 1
    If StrCategories(i) <> "" Then
        CB_Categories.Items.Add(StrCategories(i))
    End If
Next

This code works correctly.

If this empty or blank entry is selected then all entries are displayed, which is how I want it to work.

My problem is that I wanted to display something in the combobox that tells the user that ALL categories will be displayed instead of having a blank display.

So I need to have a combobox that has two columns, a hidden column that contains the values , and the other contains the text to display to the user.

I tried searching and found many posts on how to do this using a database, but these were of no use to me since I am not using a database.

There were other examples that placed a datagridview under the combobox, but I didn't like this approach.

I tried making a two column table from this list of strings that contained the values in one column and the text in the other and then binding it to the combobox with the display member set to the text and the value member set to the value.

Tbl_Categories.Rows.Add("", "All")
For i = 0 To StrCategories.Count - 1
    If StrCategories(i) <> "" Then
        Tbl_Categories.Rows.Add(StrCategories(i), StrCategories(i))
    End If
Next

For appearance this was perfect, but it didn't work because when "All" was selected the filter no longer showed all entries, but instead was looking for entries that had nothing as their category.

If I could get that to work then I would be happy.

I don't know if you can do something like "if it isn't null then display it". I think that would show everything, including an empty string.

Apparently there is something different between adding "" directly to a combobox compared to adding it to a row in a table. I think when adding it to a combobox you add a value but when adding to a table you are adding text.

I have code in the app that shows a combobox with a colored rectangle, an ID number and a color name, I had help creating this.

I tried to copy and modify it for the categories but due to a current lack of understanding I was unable to get it to work.

Because of this color combobox I am sure there is a way to create a combobox that displays one list and works off a second hidden list of values, but I don't know how to do it.

Does anybody know how to create such a combobox from a list of strings?

Developer technologies | VB
{count} votes

3 answers

Sort by: Most helpful
  1. Cynolycus 285 Reputation points
    2023-10-22T11:13:39.6166667+00:00

    Because of the code that I had in place in other parts of my app I was able to solve the problem in a different way to any of the suggestions above.

    Although my solution turned out to be quite simple it is so dependent on the rest of the code that it is too much to show here but basically if the user chooses "All" then nothing gets filtered. The only problem is if there were to ever be an instance where "All" is added in any of the columns then it won't show just those entries. For my app the chance of that happening is so remote that it wasn't worth considering, in fact it's almost impossible.

    I do want to get back to trying these suggestions as I am sure that I will need them in the future.

    But for now it is working properly so I will leave it at that and move on.

    I thank you all for your input.

    0 comments No comments

  2. Jiachen Li-MSFT 34,236 Reputation points Microsoft External Staff
    2023-10-13T03:23:08.12+00:00

    Hi @Cynolycus ,

    Since ComboBox Inherits ListControl Class, you can use ListControl.DisplayMember Property and ListControl.ValueMember Property.

    DisplayMember gets or sets the property to display for this ListControl.

    ValueMember gets or sets the path of the property to use as the actual value for the items in the ListControl.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. KOZ6.0 6,735 Reputation points
    2023-10-12T15:25:59.41+00:00

    You can use DataTable without using database.

    Dim dtCombo As New DataTable
    With dtCombo.Columns
        .Add("Display", GetType(String))
        .Add("Category", GetType(String))
    End With
    With dtCombo.Rows
        .Add("A", "A")
        .Add("B", "B")
        .Add("ALL", "")
    End With
    CB_Categories.DataSource = dtCombo
    CB_Categories.DisplayMember = "Display"
    CB_Categories.ValueMember = "Category"
    

    Here's how to retrieve the selected value:

    Dim textStr As String = cbo.Text
    Dim selectedValueStr As String = CStr(cbo.SelectedValue)
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.