how to add value to combobox item

后端 未结 6 651
半阙折子戏
半阙折子戏 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:12

    If you want to use SelectedValue then your combobox must be databound.

    To set up the combobox:

    ComboBox1.DataSource = GetMailItems()
    ComboBox1.DisplayMember = "Name"
    ComboBox1.ValueMember = "ID"
    

    To get the data:

    Function GetMailItems() As List(Of MailItem)
    
        Dim mailItems = New List(Of MailItem)
    
        Command = New MySqlCommand("SELECT * FROM `maillist` WHERE l_id = '" & id & "'", connection)
        Command.CommandTimeout = 30
        Reader = Command.ExecuteReader()
    
        If Reader.HasRows = True Then
            While Reader.Read()
                mailItems.Add(New MailItem(Reader("ID"), Reader("name")))
            End While
        End If
    
        Return mailItems
    
    End Function
    
    Public Class MailItem
    
        Public Sub New(ByVal id As Integer, ByVal name As String)
            mID = id
            mName = name
        End Sub
    
        Private mID As Integer
        Public Property ID() As Integer
            Get
                Return mID
            End Get
            Set(ByVal value As Integer)
                mID = value
            End Set
        End Property
    
        Private mName As String
        Public Property Name() As String
            Get
                Return mName
            End Get
            Set(ByVal value As String)
                mName = value
            End Set
        End Property
    
    End Class
    
    0 讨论(0)
  • 2020-12-14 05:12

    Although this question is 5 years old I have come across a nice solution.

    Use the 'DictionaryEntry' object to pair keys and values.

    Set the 'DisplayMember' and 'ValueMember' properties to:

       Me.myComboBox.DisplayMember = "Key"
       Me.myComboBox.ValueMember = "Value"
    

    To add items to the ComboBox:

       Me.myComboBox.Items.Add(New DictionaryEntry("Text to be displayed", 1))
    

    To retreive items like this:

    MsgBox(Me.myComboBox.SelectedItem.Key & " " & Me.myComboBox.SelectedItem.Value)
    
    0 讨论(0)
  • 2020-12-14 05:17

    Now you can use insert method instead add

    ' Visual Basic
    CheckedListBox1.Items.Insert(0, "Copenhagen")
    
    0 讨论(0)
  • I am assuming that you are wanting to add items to a ComboBox on an Windows form. Although Klaus is on the right track I believe that the ListItem class is a member of the System.Web.UI.WebControls namespace. So you shouldn't be using it in a Windows forms solution. You can, however, create your own class that you can use in its place. Create a simple class called MyListItem (or whatever name you choose) like this:

    Public Class MyListItem
        Private mText As String
        Private mValue As String
    
        Public Sub New(ByVal pText As String, ByVal pValue As String)
            mText = pText
            mValue = pValue
        End Sub
    
        Public ReadOnly Property Text() As String
            Get
                Return mText
            End Get
        End Property
    
        Public ReadOnly Property Value() As String
            Get
                Return mValue
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return mText
        End Function
    End Class
    

    Now when you want to add the items to your ComboBox you can do it like this:

    myComboBox.Items.Add(New MyListItem("Text to be displayed", "value of the item"))
    

    Now when you want to retrieve the value of the selected item from your ComboBox you can do it like this:

    Dim oItem As MyListItem = CType(myComboBox.SelectedItem, MyListItem)
    MessageBox.Show("The Value of the Item selected is: " & oItem.Value)
    

    One of the keys here is overriding the ToString method in the class. This is where the ComboBox gets the text that is displayed.


    Matt made an excellent point, in his comment below, about using Generics to make this even more flexible. So I wondered what that would look like.

    Here's the new and improved GenericListItem class:

    Public Class GenericListItem(Of T)
        Private mText As String
        Private mValue As T
    
        Public Sub New(ByVal pText As String, ByVal pValue As T)
            mText = pText
            mValue = pValue
        End Sub
    
        Public ReadOnly Property Text() As String
            Get
                Return mText
            End Get
        End Property
    
        Public ReadOnly Property Value() As T
            Get
                Return mValue
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return mText
        End Function
    End Class
    

    And here is how you would now add Generic items to your ComboBox. In this case an Integer:

    Me.myComboBox.Items.Add(New GenericListItem(Of Integer)("Text to be displayed", 1))
    

    And now the retrieval of the item:

    Dim oItem As GenericListItem(Of Integer) = CType(Me.myComboBox.SelectedItem, GenericListItem(Of Integer))
    MessageBox.Show("The value of the Item selected is: " & oItem.Value.ToString())
    

    Keep in mind that the type Integer can be any type of object or value type. If you want it to be an object from one of your own custom classes that's fine. Basically anything goes with this approach.

    0 讨论(0)
  • 2020-12-14 05:25

    Instead of adding Reader("Name") you add a new ListItem. ListItem has a Text and a Value property that you can set.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题