如何为组合框项添加值

时间:2009-12-11 11:17:42

标签: vb.net

如何在Visual Basic 2010中将每个项目的数据值添加到组合框?

与html下拉框类似。

或者无论如何都要为每个项目添加值?

我正在从MySQL数据库中添加项目,如下所示:

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()
        ComboBox1.Items.Add(Reader("name"))
    End While
End If

我需要添加Reader("ID")作为每个项目的值......

6 个答案:

答案 0 :(得分:14)

我假设你想要在Windows窗体上的ComboBox中添加项目。虽然Klaus在正确的轨道上,但我相信ListItem类是System.Web.UI.WebControls命名空间的成员。因此,您不应该在Windows窗体解决方案中使用它。但是,您可以创建自己的类,以便在其位置使用。 创建一个名为MyListItem的简单类(或您选择的任何名称),如下所示:

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

现在,当您想要将项目添加到ComboBox时,您可以这样做:

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

现在,当您想从ComboBox中检索所选项目的值时,您可以这样做:

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

这里的一个关键是覆盖类中的ToString方法。这是ComboBox获取显示文本的地方。


Matt在下面的评论中提出了一个很好的观点,即使用Generics使其更加灵活。所以我想知道那会是什么样子。

这是新的和改进的GenericListItem类:

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

现在,您将如何将Generic项添加到ComboBox。在这种情况下,整数:

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

现在检索项目:

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())

请记住,Integer类型可以是任何类型的对象或值类型。如果你希望它是你自己的一个自定义类的对象,那很好。基本上任何事情都与这种方法有关。

答案 1 :(得分:10)

虽然这个问题已有5年历史,但我遇到了一个很好的解决方案。

使用'DictionaryEntry'对象来配对键和值。

将“DisplayMember”和“ValueMember”属性设置为:

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

将项目添加到ComboBox:

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

要检索这样的项目:

MsgBox(Me.myComboBox.SelectedItem.Key & " " & Me.myComboBox.SelectedItem.Value)

答案 2 :(得分:9)

如果你想使用SelectedValue,你的组合框必须是数据绑定。

设置组合框:

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

获取数据:

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

答案 3 :(得分:1)

您可以添加新的Reader("Name"),而不是添加ListItemListItem具有您可以设置的TextValue属性。

答案 4 :(得分:1)

现在,您可以使用insert方法代替add

' Visual Basic
CheckedListBox1.Items.Insert(0, "Copenhagen")

答案 5 :(得分:0)

是的,在大多数情况下,您不需要创建具有getter和setter的类。只需创建一个新的Dictionary并将其绑定到数据源。这是VB中使用for循环从列表中设置组合框的DisplayMember和ValueMember的示例:

        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"

然后,您可以通过单击调用方法根据值或任何需要设置数据网格视图的行:

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