Combo add name and value

nate

Freshman
Joined
Dec 2, 2006
Messages
36
O.K.

To add items to a combo box, I know I use
Code:
cbo.items.add("myItem")

however that adds the items to the name and value fields. I want to add something different to the Value field than what is in the name field. Can I do this at runtime?
 
I don't know if I have to ask this or not. But are you developing a windows application or a web application? :D It is confusing that you're using the "combo box" term but you also mentioned the Name and Value field. :D

If you're developing a windows application. If this is true then it is out of my reach. I don't even think it's possible to do what you want.

But if you're developing a web application then you should create a new item object (the class would be ListItem if I'm not mistaken). Within that new object you can specify different values for the Name and Value field. With that new object you're now able to add it.

Hope it helps.
 
Sorry,
Long time App developer now doing alot of ASP.Net. yes it is for a web, sorry a dropdownlist. I know if I add the control dynamically I can add the value field but this is a dropdownlist already on the form. Can I still somehow reference the value field? Thanks for the help.
 
here's what Amir was talking about. when you add an item, it will allow you to add objects. so create a basic object for your "items", then add it.

dim o as New InfoObject
o.setName = "myname"
o.setValue = "myvalue"
cbo.items.add(o)

For your InfoObect, you will want get/set functions (or use properties if you can so you can just say o.name="myname"

You will also need to override the toString function and return what you want it to display. For example:
Public Overrides Function toString() as String
return me.name
End Function
in this case, your combobox will display the name of your object in the list

Finally, what this results in is if you ask the combobox for its "SelectedItem" it will return an object of the type you added, so in this case InfoObject. so you can say
o = cbo.selectedItem
name = o.getName
value = o.getValue
 
Last edited:
Back
Top