Unselect a ListBox

J

Joe Cool

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?
 
K

kimiraikkonen

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?

Joe,
This way deselectes any selected item:

ListBox1.SetSelected(0, False)

It's up to you when you trigger the event. For instance, you can place
a simple button named "clear" then:
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
ListBox1.SetSelected(0, False)
End Sub
 
M

Morten Wennevik [C# MVP]

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.
 
J

Joe Cool

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.

Thanks!! That's exactly what I needed. Works like a charm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top