|
-
Feb 11th, 2003, 10:13 AM
#1
Thread Starter
New Member
Objects in combobox
Hi!
I am working with a windows form containing a combobox (cboSchools). In the combobox I want to display a list of schools. But I want each item in the combobox to contain a little more information than only the name of the school, therefor I populate the combobox with objects of type clsSchool, a class containing name of school, id etc. Populating the combobox is no problem. With a ToString function in the clsSchool class the names of the schools are shown in the combobox.
Then I want to retreive the object that the user has selected. But Me.cboSchool.SelectedItem will return a object of type System.Object and not of type clsSchool, and thus I cannot reach the properties defined in clsSchool.
So the question is: How can I make my objects in the combobox recognised as beeing of type clsSchool? Is there some way of setting the default type of items in a combobox to something else than System.Object? Or does anyone know another way of solving this?
Thankful for any help!
/Sara
-
Feb 13th, 2003, 11:11 AM
#2
Thread Starter
New Member
Ok, if I skip the idea of having my own objects in the combobox, and instead go for string/id pairs maybe someone can help me. Then my problem is like this:
I want a combobox displaying a list of names of schools, no problems so far. But I want each name to be
connected to an id-number, and I cannot find out how to do that. The point is that when a school is selected
I should be able to pick out the corresponding id-number from some combobox property.
It feels like there should be a quite simple solution to this but I just can't figure it out.
/Sara
-
Feb 13th, 2003, 11:37 AM
#3
If you want to go with your original way of having the objects in the combo then you can access the properties (just with no intellisense) or better yet just convert it to type cSchool.
dim curSchool as cSchool=CType(cboSchools.SelectedItem,cSchool)
Also you don't have to use the ToString method you can set the DisplayMember property of the combobox to a property name of the class. If you need a Value from it you can also set the ValueMember to another property and use the SelectedValue property to retrieve it.
-
Feb 14th, 2003, 02:13 AM
#4
Thread Starter
New Member
That was just what I needed, thank you very much for your help!!!
/Sara
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|