473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Unselect a ListBox

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?
Mar 21 '08 #1
3 12688
On Mar 21, 7:41 am, Joe Cool <joec...@home.netwrote:
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
Mar 21 '08 #2
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?
Mar 21 '08 #3
On Fri, 21 Mar 2008 04:15:01 -0700, Morten Wennevik [C# MVP]
<Mo************@hotmail.comwrote:
>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.
Mar 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
3
by: Stefan Mueller | last post by:
I've an input box <input type = "text" name = "MyInput" value = ""> and a selection <select name = "MySelection" size = "1"> <option value = "1">Entry 1 <option value = "2">Entry 2 </select>...
1
by: mikev | last post by:
Hi all I have a listbox which is 5 rows high. There are 5 list items. The first one is selected. I wish to click on a button and have the selection focus on the listbox removed. Removed,...
2
by: Stefan Mueller | last post by:
I've a ListView in ReportMode with MultiSelect = False. If the user enters a number I select the appropriate item in the ListView. If the used enters a number which is not in the ListView I'd...
6
by: sesling | last post by:
I have several forms that use list boxes. These list boxes are used to pass information to command line. The operator is not required to select an item from each box. See below ListBox1 -...
1
by: =?Utf-8?B?anVsaW8=?= | last post by:
i have a listbox w/ single select mode. Can a user unselect an item by clicking on it? How is it implemented? Thanks
3
by: Joe Cool | last post by:
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...
1
by: mishasoft | last post by:
Hi all, I have a HTML multiple select (ListBox) control: <select id="mySelect" multiple> <option>Apple</option> <option>Orange</option> <option>Pineapple</option> ...
1
by: rbr | last post by:
I have two listboxes on an asp.net web control (ascx). I want them to act non-mutually-exclusive. In other words, if an item in one listbox is selected, I want the other to be clear. The problem...
4
by: kashif73 | last post by:
Hi , I have a combobox in my form. If the user select some option from the combobox, it subsequently populate Listboxes (5 in all) on my form. This works fine. the annoying part is that in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.