在Visual Basic中将对象列表添加到Dropdown Combobox

时间:2015-02-13 14:50:22

标签: vb.net list collections combobox

我是Visual Basic的新手,主要通过反复试验来创建,但我现在已经尝试了大约5个小时并没有运气。我正在尝试为赛跑者创建一个用于赛事的程序。它有多种形式。创建Runners和Races有两种形式。然后将它们存储在Runner和Race Collection Lists中。我想用一个存储在种族收集列表中的种族填充一个下拉框。到目前为止,我必须实现这个目标的是显示“{}集合”的下拉列表。我试过.datasource,.add和.addRange。似乎没有用。

我的种族收集代码是:

Public Class RaceList
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal aRace As Race, Optional ByVal key As String = "NewRace")

        List.Add(aRace)
    End Sub
    Public ReadOnly Property Item(ByVal index As Integer) As Race
        Get

        Return CType(List.Item(index), Race)
    End Get
End Property
End Class

它应该只允许用户添加并返回列表中的种族。

以下是允许用户将竞赛添加到列表中的代码:

Public Class newRaceForm
    Public Shared racelist As New RaceList
    Private Sub uiBtnAddNewRace_Click(sender As System.Object, e As System.EventArgs) Handles uiBtnAddNewRace.Click

        uiDTPRaceDate.Text = Today

        Dim x As Date
        Dim champ As Boolean = False
        x = uiDTPRaceDate.Text
        If uiCheckboxChampion.Checked = True Then
            champ = True
        End If

        Dim race As New Race(x, champ)

        uiCheckboxChampion.Checked = False

        MsgBox("Race Added")
        racelist.Add(race, race.uniqueRaceID)
    End Sub

最后,这是form_load上的代码,它应该用racelist的内容填充该框。

Private Sub finishRaceForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim x = 0
    Dim races As New RaceList

    While x < races.Count
        uiDropDownRace.Items.Add(races.Item(x).ToString)
        x = x + 1
    End While

End Sub

此外,我的比赛类是这样创建的:

Public Class Race
Private raceDate As String
Private isChampionship As Boolean
Public Shared RaceID As Integer = 0
Public uniqueRaceID As String


Sub New(ByVal x As String, champ As Boolean)

    raceDate = x

    If champ = True Then
        isChampionship = True
    Else
        isChampionship = False
    End If

    RaceID = RaceID + 1
    uniqueRaceID = "RaceID0" + RaceID.ToString

End Sub
End Class

2 个答案:

答案 0 :(得分:1)

无论何时将对象添加到列表框或下拉列表中,都会调用ToString函数来确定要显示的内容。

大多数对象默认返回其类型名称作为其ToString函数。您可以覆盖ToString函数以显示您想要的任何内容。在下面的示例中,我显示文本“Race Number x”,其中x是比赛号码。

Public Class Race
    Private raceDate As String
    Private isChampionship As Boolean
    Public Shared RaceID As Integer = 0
    Public uniqueRaceID As String


    Sub New(ByVal x As String, champ As Boolean)
        raceDate = x

        If champ = True Then
            isChampionship = True
        Else
            isChampionship = False
        End If

        RaceID = RaceID + 1
        uniqueRaceID = "RaceID0" + RaceID.ToString

    End Sub
    Public Overloads Function ToString() As String
            Return "Race Number " & RaceID.ToString()
End Function
End Class

答案 1 :(得分:1)

我正在研究类似的程序并使用它来使其工作。

Dim CmbAcro As String() = {"INSERT", "THE", "ITEMS", "YOU", "WANT", "TO", "ADD", "TO", "A", "COMBO", "BOX"}

Dim cmb As New DataGridViewComboBoxColumn()

cmb.HeaderText = "INSERT HEADER TEXT HERE"
cmb.Name = "INSERT NAME HERE"
cmb.MaxDropDownItems = 20
cmb.Sorted = True

For Each i In CmbAcro
                cmb.Items.Add(i)
            Next
            DataGridView1.Columns.Add(cmb)