Programatically select listbox rows

This works in a weblistbox:

  case is <= 5
    for i as integer = 0 to 4
      me.selected(i) = true
    next i

It doesn’t work in listbox. Why not? And, what will work?

note: I had to click ctrl-C four times to copy this text from the IDE.

Did you set the Listbox.SelectionType to SelectionMultiple (1)? You need to do this to select multiple rows in a ListBox.

yep.

[quote=68064:@John Scanlan]case is <= 5
for i as integer = 0 to 4
me.selected(i) = true
next i
It doesn’t work in listbox. [/quote]

I suppose you placed this in the open event. You need to populate the listbox before selecting.

Here :

me.addrow "apple" me.addrow "pear" me.addrow "cherry" me.addrow "avocado" me.addrow "banana" me.SelectionType = 1 for i as integer = 0 to 4 me.selected(i) = true next i

Try to hold a bit when pressing Ctrl-C, or simply use the contextual menu

[quote]I suppose you placed this in the open event. You need to populate the listbox before selecting.

[/quote]

That can’t possibly be the mistake the OP made as he would have received an outofboundsexception.

Then it must be the case is <= 5 . It’s now up to the OP to explain better.

select case row
case is <= 5
for i as integer = 0 to 4
me.selected(i) = true
next I
else . . .

my programming is the same as Michel’s except mine is called from a cellclick.

Then return TRUE from CellClick. Otherwise the system will process the normal action of the click - unselect all the rows and select the one you just clicked.

You need to return true from the cellclick event to prevent the standard row selection from happening.

Beat me by 13 seconds, Tim.

thanks … that worked.

how about if I then select in the already selected rows? right now it changes 5 selected rows to just one.

Programmatically or by clicking on it?

If you want to deselect only one row, hold cmd while you click on it.

Given his code that’s not likely to work unless he specifically skips it when CMD/CTRL is held.

Don’t want to deselect a row. I want to deselect all 5 rows or ignore the selection.

for i as integer = 0 to 4 me.selected(i) = false next I