Hi Joe,
The ListBox does not natively support this functionality, but depending on
how the items are displayed (multiple columns etc) you can set
SelectedItems.Clear() if you find out you have clicked outside any item.
You need to either handle the MouseDown or the MouseClick event. Using
MouseDown in a ListBox with blank space at the bottom you can determine if
the mouseposition is below the last item
void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Y listBox1.ItemHeight * listBox1.Items.Count)
listBox1.SelectedItems.Clear();
}
This won't work if you have custom drawn the items using variable heights.
If you want to deselect an item if you click to the right of it you then
need to find the correct item and figure out if e.X is on the right side of
the item's bounds.
--
Happy Coding!
Morten Wennevik [C# MVP]
"Joe Cool" wrote:
I have a ListBox on a form than has some items. The select mode is one
item only. I select one, it is selected and hightlighted. I then click
somewhere in the listbox not on any item, in the clear area of the
listbox. I would like to unselect the selected item but I cannot
determine which event to use to trap this event and unselect the
current seleted item.
Any help?