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

ListBox Tag Value

Status
Not open for further replies.

mdwg2020

Programmer
Feb 17, 2005
51
US
Apparently there is no way to specify tag information for an element in a listbox, is there any other way that I can associate data with the listbox items. For instance, I already have the string that I would like to have displayed, but I would like to have a tag type element that I can store a database key for each listbox item. That way, if someone selects that particular item, I can get the information that I need out of a database

Any ideas?

Brooks
 
I beleive (may be wrong) but Listboxes (and comboboxes) have a display field and a value field. So you could create a data table that contains your string value and the key value, then bind it to the list box, then set the display field and value field.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
A ListBox contains Objects, not just strings. If you don't want to do data-binding, then you can create your own class with two properties, one for the display text and one for the value. Add those to the ListBox.
 
Riverguy's got it - just make sure when writing the class to override the function ToString() so that it will display correctly.
 
Thanks Guys, I actually began creating a listbox class that has the additional properties, so hopefully I will not have any problems with that.

I have another question concerning using data binding. Since I already have a table in the dataset with the information, what is the process for binding the data instead using an inherited class, and which would be better or easier.

Thanks, I have the class override setup from a sample that I saw somewhere else.
 
Well, I haven't been successful yet. I attempted to do the databinding by binding DisplayMember and ValueMember to a dataset table that I have. However, when I run the program, nothing appears in the ListBox. I'm not sure what I am suppose to do at this point. I believe I binded the data correctly, but I can't get anything to show up. Any suggestions?

Thanks,

Brooks
 
Post code. make sure you set the listbox.datasource = data table, then set the display and value members.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Hey Rick,

This is what I attempted to do:

<code>
lstCourses.DataSource = dsResults.Tables(2)
lstCourses.DisplayMember = "LEDef_Name"
lstCourses.ValueMember = "LEDef_PK"
</code>

And this loaded my ListBox (lstCourses) perfectly, however, I couldn't get the data back out of it. If I did a debug.WriteLine on either the SelectedItem, DisplayMember, or ValueMember, I only got what they bindings were, such as LEDef_Name, LEDef_PK, and System.DataRow I believe. I'm obviously not doing something right!
 
I also apparently forgot how to post code, I thought I could just use <Code>Tags</Code>.
 
you'll want to use '[' in stead of '<'

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top