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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox Storing Ojbects

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
I have a combo box that contain objects from an instantiated class as p-code below. How do I get the selected object as well as set the selected object?

Class comboObj
Public name as string
Public value as integer
...
End Class

...
Dim item1 as New comboObj("name1",1);
Dim item2 as New comboObj("name2",2);
'I build the combo box (MyComboBox) as follow:
Me.MyComboBox.Items.Add(item1);
Me.MyComboBox.Items.Add(item2);

'Say some value in my program requires the second value to display as the selected value. How would I get item2 to display in the combobox as the selected value? If the user selected a new value in the combobox, how would I retrieve that value/object (comboObj)? Not just the displayed value but I want to retrieve the object (comboObj).

Hopefully that all makes sense.

Thanks,
Greg
 
Code:
combobox.SelectedIndex = IdxValue

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Sure that will set the selection to whatever index value I want...Ok. How do I retrieve the selected object from the combobox though?
 
Code:
ItemID = combobox.SelectedValue [COLOR=green]'Pulls the Displayed Value from the combo box.[/color]
ItemName = combobox.Text [COLOR=green]'pulls the value placed in the ValueMember property that is associated with the above.[/color]

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
What if I have the combo box populated as in my first post (but with more values) and I don't know what the index is of the item I want to select in the combo box. I just have the values. Say I know I want to set the currently selected value to: comboObj("nameXYZ", 100). How would I set that object to the selected value without knowing the index?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top