Deselect Selecteditem in a Listbox

ghislain

New Member
Licensed User
Hello!

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?

Ghislain
 

Cableguy

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

Attachments

  • teste.zip
    1.1 KB · Views: 211

specci48

Well-Known Member
Licensed User
Longtime 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 ...


specci48
 

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..
Thanks!
 

Cableguy

Expert
Licensed User
Longtime 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..
 

Big JR

Member
Licensed User
Longtime 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
ErrorLabel(ErrHandler)
EventsList.SelectedIndex = -1
return
ErrHandler:
End Sub

If your deselection comes within other code you'll need to have a goto to just after the selectedindex = -1 code.
 
Top