Deselect Selecteditem in a Listbox

Discussion in 'Questions (Windows Mobile)' started by ghislain, Jun 27, 2007.

  1. ghislain

    ghislain New Member Licensed User


    I'm still working on my selection form..
    Which has got a listbox. When nothing is selected the selectedindex = -1.
    Now when i select something i can't deselect it anymore..
    So I made up this deselect button with the following click-event:

    myListbox.selectedIndex = -1

    But this produces an [runtime] error everytime.
    Does anybody have a suggestion?

  2. Erel

    Erel Administrator Staff Member Licensed User

    Try to remove the selected item and then insert it again.
  3. Cableguy

    Cableguy Expert Licensed User

    If You don't require multiple selections then a radio button should be best for you....Check this code:

    Attached Files:

  4. specci48

    specci48 Well-Known Member Licensed User

    Hi ghislain,

    if I want to give the user the chance to deselect a listbox entry I'll do the following:
    I add a "dummy"-entry at the first (or last) position of the listbox named "<no selection>" (or something like that) and pre-select this entry before showing the listbox.
    Now you can proof during runtime, if the first (or last) entry is selected, which means, nothing is selected ...

  5. ghislain

    ghislain New Member Licensed User

    Thanks for all the replies.
    For the time being I will just refill my listbox. Which does the same trick as well. But it might become too slow when my list becomes longer and longer..
    I hope in the future the functionality to deselect-all will be implemented..
  6. Cableguy

    Cableguy Expert Licensed User

    Have you tryied to set the index to -1?
    That is , at least i think it is, the start point of any un-selected index..
  7. Big JR

    Big JR Member Licensed User

    Ghislain started off setting the index to -1 but, as mentioned, that gives a runtime error. If you trap that error with an empty error handler you'll get the deselection without the error being reported.

    Sub EventsList_LostFocus
    EventsList.SelectedIndex = -1
    End Sub

    If your deselection comes within other code you'll need to have a goto to just after the selectedindex = -1 code.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice