Attemping to insert an object (KeyValueItem) into a combobox rather than a string. This is for when I add items to the combobox at run time from a db, I can identify which corresponding row (i.e. no data binding) in the database has been selected in the combobox.
Here is the KeyValueItem class (perhaps something like this already exists in the framework?). When I create a new instance, I pass in the database ID and a string.
Public Class KeyValueItem
Dim key As Integer
Dim value As String
Sub New(ByVal keyIn As Integer, ByVal valueIn As String)
key = keyIn
value = valueIn
End Sub
Public Overrides Function ToString() As String
Return value
End Function
Public Function ID() As Integer
Return key
End Function
End Class
All the items that I draw from the database seem to be added to the combobox OK when i step through the executing code:
For Each row As DataRow In PopulateMonthNamesMeta().Rows
Me.SelectScheduledServiceMonthComboBox.Items.Add(New KeyValueItem(CInt(row.Item("ID")), row.Item("name")))
Next
Where SelectScheduledServiceMonthComboBox is the name of the combobox.
The problem is: None of the object's "value" string variable are displayed. Instead, what does appear is: Service.Database+KeyValueItem
Service is the name of my Project, KeyValueItem is the class, and Im not sure where the "Database" is coming from.
Does anyone know how to ensure the overridden ToString() function is used (instead of whatever is at the moment) when the object is added to the combobox collection?
Any help much appreciated
Thomas Griffiths
tom@vscan.org
Here is the KeyValueItem class (perhaps something like this already exists in the framework?). When I create a new instance, I pass in the database ID and a string.
Public Class KeyValueItem
Dim key As Integer
Dim value As String
Sub New(ByVal keyIn As Integer, ByVal valueIn As String)
key = keyIn
value = valueIn
End Sub
Public Overrides Function ToString() As String
Return value
End Function
Public Function ID() As Integer
Return key
End Function
End Class
All the items that I draw from the database seem to be added to the combobox OK when i step through the executing code:
For Each row As DataRow In PopulateMonthNamesMeta().Rows
Me.SelectScheduledServiceMonthComboBox.Items.Add(New KeyValueItem(CInt(row.Item("ID")), row.Item("name")))
Next
Where SelectScheduledServiceMonthComboBox is the name of the combobox.
The problem is: None of the object's "value" string variable are displayed. Instead, what does appear is: Service.Database+KeyValueItem
Service is the name of my Project, KeyValueItem is the class, and Im not sure where the "Database" is coming from.
Does anyone know how to ensure the overridden ToString() function is used (instead of whatever is at the moment) when the object is added to the combobox collection?
Any help much appreciated
Thomas Griffiths
tom@vscan.org