Multi listbox selection with contextual menu


I would like to have multiple selections in a listbox and then use a contextual menu to apply “something” to the selection… but when the conteual menu is built the selection in the listbox reverts to one item (the first). Is there any way to retain the multiple selection, repress the reset of the selection etc? It seems to reset immediately in the ConstructContextualMenu event, so I can’t grab the selection before appending to the base menu.


what is your constructcontextualmenu code ?
I am able to get the contextualmenu in a listbox with multiple selection
so your code must be the cause of the trouble

dim mymenu as new MenuItem
base.Append mymenu
return TRUE

OSX 10.11.6, but seems the same to me with Windows 10

hey, it seems you’re right …
if you ctrl-clic the mouse the selection is retained
if you right-clic the selection is erased and only one line is selected …
seems it needs a bug report !

Seems it already is #1705… has been around awhile

it seems we will have to wait to have it corrected…
during that time you can ctrl-clic it works the way you want.

In Feedback, type Listbox in the search a bug report field,

Wait for the list, Click in a list entry, Select all, Copy, Paste (where you want, but where you can read the # of entries)

Result: 521 entries

IMHO: a whole build of a brand new Listbox may be faster to do than squashing all these bugs (yes, IMHO: I may be wrong).

Faster probably not. The listbox is all custom code. AFAIK it’s not relying on any native controls to work.

I think what I would prefer would to have a less generic listbox and split it up into different, more specific, controls: Listbox, Hierarchical ListBox/Treeview, Grid (with containers).

We all spend way to much time customizing the ListBox and while it works I’m not sure anyone is especially happy with it. But then again I (and many others) have been wishing for a more powerful Listbox for a long time.

Keep it simple.

  • Add a ListBox1Selected() as Boolean to the window
  • In ListBox1.MouseDown :

[code]Function MouseDown(x As Integer, y As Integer) As Boolean
Redim ListBox1Selected(-1)

for i as integer = 0 to listbox1.ListCount-1

End Function

  • In ListBox1.ConstructContextualMenu

Function ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) As Boolean for i as integer = 0 to ListBox1Selected.Ubound-1 Listbox1.selected(i) = ListBox1Selected(i) next

Voila :slight_smile:

Any grid control is a catch 22.

Make it powerful and very complete, and it becomes a nigtmarish, very complex control. Think NSTableView which by the admission of anybody who tried to implement it in Xojo lost a few hairs in the process.

Make it simpler to use, a lots of people will be able to master it relatively easily, and if necessary, expand it with some efforts.

I think Xojo wisely picked the second idea. Why they did not chose to implement a simplified version of the native control probably has to do with the necessity to have the same control cross platform.

I am reasonably sure it satisfies a vast majority or beginners, who appreciate the low learning curve. Of course, as one gains in competence, barriers appear that soon become annoying.

NSTableView implementations such as dtPlugins and the new control from Rob Egal do exist. But as the later posted while he was developing, he gained some appreciation for Xojo’s engineers. Difficult to make simple with complex.

@Michel Bujardet [quote]Keep it simple.[/quote]
Although I think the bug meantime has been fixed, for me, still using 2016v1.1, your solution has proved excellent.
Thank you.

[quote=335635:@Carlo Rubini]@Michel Bujardet
Although I think the bug meantime has been fixed, for me, still using 2016v1.1, your solution has proved excellent.
Thank you.[/quote]
Yes. This behavior changed in 2017r1. Now, a contextual click will not change the selection.

Thanks for confirming my assumption.

The good thing about a well conceived workaround, is that it does not break when the bug that justified it is fixed.

Return True in the MouseDown event of the listbox should work.

[code]Function MouseDown(x As Integer, y As Integer) Handles MouseDown as Boolean

If IsContextualClick Then Return True
End Function[/code]

@Stefan Adelsberger I had already tried it: multiselection is OK, but no contextualmenu shows up.

I tried this and for me it works:

[code]Function ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) Handles ConstructContextualMenu as Boolean

base.Append New MenuItem(“test”)

Dim Result As MenuItem = base.PopUp
End Function