OutOfBoundsException when focus is out of the listbox item

Hello Experts,
I have a simple code in the CellClick event of a listbox:

#If TargetWindows Then If Keyboard.AsyncControlKey Then dim t as folderitem = GetFolderItem(Me.RowTag(Me.ListIndex), FolderItem.PathTypeShell) If t <> nil and t.Exists then if t.Directory then t.launch else t.parent.launch End if End If End If #Elseif TargetMacOS Then If Keyboard.AsyncCommandKey Then dim t as folderitem = GetFolderItem(Me.RowTag(Me.ListIndex), FolderItem.PathTypeShell) If t <> nil and t.Exists then if t.Directory then t.launch else t.parent.launch End if End If End If #Endif
The Listbox.RowTag inhabit the shell path of a previously dropped file onto the listbox and already been checked and double checked.
The odd thing is when I click+Ctrl(win) or click+cmd(mac) while the listbox item is “in focus” everything works perfect, but when I do the same action while the focus is on the window or completely outside of the app the debugger jumps with an OutOfBoundsException.
I tried to set the focus to the listbox item but couldn’t make it work, Even tried to add ‘Try…Catch’ like so:

If Keyboard.AsyncControlKey Then Try dim t as folderitem = GetFolderItem(Me.RowTag(Me.ListIndex), FolderItem.PathTypeShell) If t <> nil and t.Exists then if t.Directory then t.launch else t.parent.launch End if End If Catch er As RuntimeException If er IsA NilObjectException Then Window1.Messages_Area.Text = "First select the item then ctrl+click it" Else Raise er End If Catch eo As OutOfBoundsException Window1.Messages_Area.Text = "First select the item then ctrl+click it" End Try End If
But that didn’t help either.
I just want to get rid of the OutOfBounds I don’t care if it doesn’t do what it’s supposed to do when out of focus (well, it would be nice too of course, but in second priority for now)
Open to ideas.

If Keyboard.AsyncControlKey and me.ListIndex <> -1 Then

Beautiful !!
Thanks :slight_smile:

In that case I think I should go with:

If Keyboard.AsyncControlKey and Me.ListIndex >= 0 Then

It’s a bit more accurate no, or is it just the same ?

From what I understand, values could be >= -1, so basically <> -1 and >= 0 will work the same. I guess you can use > -1 too.

Nice Alberto,
So from that I’m guessing that the only “field mine” is -1 anything else is workable.
Thanks, very good to know :slight_smile: