C#: Using ListBoxes

ListBoxes are those same drop down components that you see in HTML forms. The same control called ComboBox works the same way although the appearance would be that of a 1 liner drop down box. Anyway, adding items to a ListBox object is pretty easy. Just do

But the code above will only add the labels that will apear within the ListBox. What if you want to have a ListBox that acts the same as a drop down HTML form component with a name and value attribute. You can create your own custom class with Name and Value exposed, place them inside an ArrayList object and pass it on to the ListBox’s DataSource property.

You can then get the selected name (and/or value) of the ListBox by doing this

Related Posts Plugin for WordPress, Blogger...

2 comments

  1. There is an Error on Line 11:
    public override String toString()

    override will only work if you name “ToString()” correctly:
    public override String ToString()

  2. @sepp: thanks for the heads up. i cannot verify this since it has been quite a long time since i touched .net but thank you for the correction.

Leave a Reply

Your email address will not be published. Required fields are marked *