how to add value to combobox item

后端 未结 6 695
半阙折子戏
半阙折子戏 2020-12-14 04:47

How can I add data value of each item to combobox in Visual Basic 2010?

Like html drop-down box.

Or is there anyway to add values to each item ?

I am

6条回答
  •  一个人的身影
    2020-12-14 05:27

    Yeah, for most cases, you don't need to create a class with getters and setters. Just create a new Dictionary and bind it to the data source. Here's an example in VB using a for loop to set the DisplayMember and ValueMember of a combo box from a list:

            Dim comboSource As New Dictionary(Of String, String)()
            cboMenu.Items.Clear()
            For I = 0 To SomeList.GetUpperBound(0)
                comboSource.Add(SomeList(I).Prop1, SomeList(I).Prop2)
            Next I
            cboMenu.DataSource = New BindingSource(comboSource, Nothing)
            cboMenu.DisplayMember = "Value"
            cboMenu.ValueMember = "Key"
    

    Then you can set up a data grid view's rows according to the value or whatever you need by calling a method on click:

    Private Sub cboMenu_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboMenu.SelectionChangeCommitted
        SetListGrid(cboManufMenu.SelectedValue)
    End Sub
    

提交回复
热议问题