【问题标题】:Adding List of Objects to Dropdown Combobox in Visual Basic在 Visual Basic 中将对象列表添加到下拉组合框中
【发布时间】:2015-04-14 15:32:01
【问题描述】:

我对 Visual Basic 还是很陌生,主要是通过反复试验来创建,但我已经尝试了大约 5 个小时,但没有运气。我正在尝试创建一个用于跑步者活动的程序。它有多种形式。创建 Runners 和 Races 有两种形式。然后将这些存储在 Runner 和 Race Collection List 中。我想用存储在比赛集合列表中的比赛填充一个下拉框。到目前为止,我要实现这一目标的壁橱是显示“{}collection”的下拉菜单。我尝试过 .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

【问题讨论】:

  • 发生了什么故障以及在哪里/在哪一行?
  • 完全没有失败或错误。这一切都运行了,我可以添加一场比赛(我认为)但是当我进入下拉表单时,它永远不会填充任何内容,只是一个空白下拉列表
  • 据我所知,这是因为您没有在下拉字段(我认为是组合框)中填充比赛。在 form.load 上,没有任何,后来你没有添加创建的比赛。
  • 这就是这个循环应该做的: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 &lt; races.Count uiDropDownRace.Items.Add(races.Item(x).ToString) x = x + 1 End While End Sub
  • 但这不是循环的作用。当 Form.Load 被执行时,它会检查races.count,发现它为零,将它与 x 进行比较,x 是零,甚至不进入循环。此外,Form.Load 也不适合这项工作。您应该在 button_click() 函数中调用 .Add。

标签: vb.net list collections combobox


【解决方案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)

【讨论】:

  • 欢迎堆栈溢出!感谢您提供似乎是一个很好的答案。随时查看帮助 --> 站点游览以获得免费徽章!
【解决方案2】:

每当您将对象添加到列表框或下拉列表时,都会调用 ToString 函数以确定要显示的内容。

大多数对象默认返回它们的类型名称作为它们的 ToString 函数。您可以覆盖 ToString 函数以显示您想要的任何内容。在下面的示例中,我显示文本“比赛编号 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

【讨论】:

  • 在这个例子中,你将如何修改它以使用 Races 列表。我有New RaceList as New List(of Race),当用户通过单击按钮创建比赛时,我将比赛传递给它。我想用比赛日期填充下拉列表或列表框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多