Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox - Add Object (Rather than string)

Status
Not open for further replies.

revz

Technical User
Apr 18, 2001
10
0
0
GB
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
 
Use this to load up your combobox



Dim dsRow As DataRow

For Each dsRow In dsItems.Tables("Items").Rows

cboItem.Items.Add(dsRow(0)) ' & ", " & dsRow(1))

'& " - " & dsRow(1)
Next

the number next to dsRow is the column in the dataset, you don't need to reference the name of the row, that might be hanging you up. I hope this helps
Sincerely,
Fritts
 
Hi Fritts,

Thanks for your reply :)

I tried referencing by index rather than name but the problem still occured.

What my friend discovered was that when we restarted visual studio, and ran the project straight away, the problem did not occur, i.e. the combobox was being populated as intended. When we BUILD the solution, REBUILD the solution, OR BUILD the project, the problem does not occur. When we REBUILD the project, not the solution, this is when the problem does occur.

Does this sound familiar to anyone?


Thomas Griffiths
tom@vscan.org

 
Thomas,
I've never had a problem like that, i check around and see if anybody else in my office has.

Sincerely,
Michael
 
Hi Michael,

Our solution was to remove the tostring() override and to add a readonly property "Value" to the keyvalueitem. Then for each combobox that I was adding keyvalueitems to, set the combobox property DisplayMember = "Value".


Thanks for all your time, Michael


Thomas Griffiths
tom@vscan.org

 


At what place should this code be placed??



For Each dsRow In dsItems.Tables("Items").Rows

cboItem.Items.Add(dsRow(0)) ' & ", " & dsRow(1))
'& " - " & dsRow(1)
Next


 
I used it on form load


Thomas Griffiths
tom@vscan.org
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top