Reputation: 195
I am pretty new to Visual Basic and mostly create through trial and error but I've been attempting this for about 5 hours now and have had no luck. I am trying to create a program used at events for runners. It has multiple forms. There are two forms to create Runners and Races. These are then stored in the Runner and Race Collection Lists. I want to populate a dropdown box with the races that are stored in the race collection list. The closet I have got to achieving this so far is the dropdown displaying "{}collection". I have tried .datasource, .add and .addRange. None seem to work.
My race collection code is:
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
it should simply allow the user to add too and return races in the list.
Here is the code that allows the user to add a race to the list:
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
Finally, Here is the code on the form_load that should populate the box with the contents of 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
Also, my races class is created as such:
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
Upvotes: 1
Views: 4516
Reputation: 952
I'm working on a similar program and got it to work by using this.
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)
Upvotes: 1
Reputation: 908
Whenever you add an object to a list box or drop down list, the the ToString function is called, to determine what is going to be shown.
Most objects default to returning their type name as their ToString Function. You can overrride the ToString function to display whatever you wish. In the example below, I display the text "Race Number x" where x is the race number.
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
Upvotes: 1