Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Unselect a ListBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Cool - 21 Mar 2008 06:41 GMT
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?
kimiraikkonen - 21 Mar 2008 10:29 GMT
> 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
[quoted text clipped - 4 lines]
>
> 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
Morten Wennevik [C# MVP] - 21 Mar 2008 12:15 GMT
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.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> 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
[quoted text clipped - 4 lines]
>
> Any help?
Joe Cool - 21 Mar 2008 20:29 GMT
>Hi Joe,
>
[quoted text clipped - 17 lines]
>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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.