CODECUBE VENTURES

WinForm ComboBox with 2 values

I've never understood why Windows form listboxes can only hold one value (the one that's displayed). That's one of the features that an HTML listbox has over it's winform cousin. I was searching for a way of doing this, and I found it.

Using an array, you can easily populate both the listbox, and the array during the loop.


private ar as ArrayList = new ArrayList()
private sub form_load(o as object, e as eventargs)
'populate you datareader using your favorite method
do while dr.Read()
listBox.Items.Add(dr("name"))
ar.Add(dr("id"))
loop
end sub

Then you can access the ID of the selected record like so


MessageBox.Show(ar(ListBox.SelectedIndex))

Hope that helps someone :)

Latest post: Digging Up the First Version of CodeCube

See more in the archives