Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding objects to list box in .NET
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00846761
Message ID:
00846937
Views:
26
Hi Tim,

I assume you are referring to WinForms. There is not straight-forward to accomplish. However, one work-around is to create a new class for the listitem and store the value information in it. You would then override the ToString method to provide how you wanted the information displayed. Here is an example:

Create a class that contains whatever information you want. Override
that class' "ToString" property to display whatever you want to display
in the dropdown. Then just add that class to the dropdown. Example:
Public Class Employee
 public emp_name as string
 public emp_age as integer
 public emp_hiredate as date

 public overrides function ToString() as String
  Return emp_name
 end function
End Class
Now, if you have an instance of "Employee" (letys say "emp1"), you can:
ddList1.Items.Add(emp1)
To get the employee selected in the list use:
emp1 = CType(ddList1.SelectedItem, Employee)
>I am working on an app in .NET (VB.NET). I want to display a list box with text and the value of the selected item to be from my database field. I am aware that you add objects to the list box as items. Something like this is what I have in mine....
>
>dim obj as new clsListItem
>obj.display = "text to display"
>obj.itemValue = 12345
>lstProducts.items.add(obj)
>
>How do I have my text display properly? When I get the selected item, will it return the object?
>
>Thanks in advance!
>Tim
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform