Scott
Scott

Reputation: 3732

VB.NET Combo Box with multiple fields to select from

I'm porting an Access database I wrote over to VB.NET and while I've used Access for 25 years, I've used VB.NET for 2 days so I'm unclear what the capabilities are.

In my Access form, I have a Combo Box which allows the user to select a part record from a table. Using Access I can show multiple columns to the user making a selection - PartID, Description, notes, whatever. However once the part is selected and the combo box is closed, I show only the unique part ID. The other columns help the user choose but make the form look really cluttered.

In my VB.NET form, I set the ValueMember to the PartID. But I only know how to set the DisplayMember to a single field. I created a composite field "PartID | Description | Notes" but then that's what appears even after the selection is made.

Is it possible in VB.NET to show one thing when the box is open and another thing once the selection has been made?

    qry = "SELECT PartID, cast(PartID as string) & " | " & [partName] AS PartString
            FROM [Part_List]
            GROUP BY PartID, cast(PartID as string) & " | " & [partName]
            ORDER BY PartID;"
    cmd = New SqlCommand(qry, conn)
    adapter = New SqlDataAdapter(cmd)
    Dim tblPN As New DataTable()
    adapter.Fill(tblPN)

    ChoosePartNum.DataSource = tblPN
    ChoosePartNum.DisplayMember = "PartString"
    ChoosePartNum.ValueMember = "PartID"
    ChoosePartNum.SelectedIndex = -1

Upvotes: 0

Views: 870

Answers (1)

Kate
Kate

Reputation: 1836

I see what you are trying to do. I was doing that in MS Access too but it was more than 20 years ago.

I agree with Jimi. From a design point of view, trying to reproduce Access-like features in a new environment is not a great idea.

Users are familiar with Winforms controls and have certain expectations in term of UI layout and consistency. If you are building a new UI from scratch you should take the opportunity to redesign it and modernize it. Don't just carry 25-year old habits over. Especially if you are not the only user of that application, because you don't want people to think that your coding methods are stuck in the 20th century...

Is it possible in VB.NET to show one thing when the box is open and another thing once the selection has been made?

If you really want to achieve this behavior you need to use combo box events. The two events you need would be DropDownClosed and DropDown.

Here is how it can be done:

  Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown
        With Me.ChoosePartNum
            .DisplayMember = "composite field"
        End With
    End Sub

    Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed
        With Me.ChoosePartNum
            .DisplayMember = "PartID"
        End With
    End Sub

The trick is to change the DisplayMember property whenever the combo box is folded/unfolded by the user. The underlying value remains the same.

I don't know if you require autocomplete from keyboard or have other needs. A treeview or listview sound like obvious alternatives. But you said you want a 'compact' control. Here you go.

Upvotes: 1

Related Questions