how to add value to combobox item

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

提交回复
热议问题