unselect item in listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have a listbox w/ single select mode. Can a user unselect an item by
clicking on it? How is it implemented?

Thanks
 
You could try implementing a selected index changed handler.

I think this will fire if you select an item that's already selected and in
your handler, if the item just selected is the same as the last selected
item, set SelectedIndex to -1.

Something like this :

private int lastindex = -1;

private void mylist_SelectedIndexChanged(object sender, EventArgs e)
{
if (mylist.SelectedIndex == lastindex)
{
lastindex = -1;
mylist.SelectedIndex = -1;
return;
}

lastindex = mylist.SelectedIndex;

}

I must stress I haven't tried this; it's just a sketch. Anyway, give it a
spin; see if it works.

HTH,

Adam.
=====
 
Back
Top