views:

226

answers:

2

How can I retrieve the value of a clicked item in a multi-select listbox?

Currently, if I click one item and use lstBox.SelectedValue, it will return the proper value; however, if I then click on another item, I am still shown the first item's value.

Basically, I want the value of the item most recently clicked on, regardless of whether or not it is the SelectedValue.

+1  A: 

If it is a multiple selection listbox, you can get a collection of all the selected items by using SelectedItems instead of SelectedItem.

If you need to know the sequence in which the items were selected, or which was selected most recently, I think you would need to record if yourself by SelectedIndexChanged event.

Colin Pickard
A: 

The SelectedIndexChanged handler will get called when you select/unselect an item in the listbox.

However, it doesn't indicate which one was selected/unselected.

listbox1.SelectedItems

will contain the currently selected items and you could internally keep track of which index was most recently added.

itsmatt