Unselect a ListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Cool

    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?
  • kimiraikkonen

    #2
    Re: Unselect a ListBox

    On Mar 21, 7:41 am, Joe Cool <joec...@home.n etwrote:
    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.SetSel ected(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.EventArg s) Handles btnClear.Click
    ListBox1.SetSel ected(0, False)
    End Sub

    Comment

    • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

      #3
      RE: Unselect a ListBox

      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.C lear() 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_MouseD own(object sender, MouseEventArgs e)
      {
      if (e.Y listBox1.ItemHe ight * listBox1.Items. Count)
      listBox1.Select edItems.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?
      >

      Comment

      • Joe Cool

        #4
        Re: Unselect a ListBox

        On Fri, 21 Mar 2008 04:15:01 -0700, Morten Wennevik [C# MVP]
        <MortenWennevik @hotmail.comwro te:
        >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_MouseD own(object sender, MouseEventArgs e)
        {
        if (e.Y listBox1.ItemHe ight * listBox1.Items. Count)
        listBox1.Select edItems.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.

        Comment

        Working...