Right click a listbox

Frustrated with popup menus in a listbox

If I have a list of 100 items, and I select row 6

Then scroll to be able to see row 70
Then right click on row 70

The Change event doesnt fire quickly enough to change the listindex.
So the popup menu thatI display (either in mousedown or in cellclick.I have tried both) that wants to ‘do something with selected row’ is actually going to work on the wrong row.

Worse, if I record the listindex before the code in the mousedown event, and try to reset the listindex afterwards, it lands on the wrong row… almost like it is determining the new row based on the position of the mouse as it is after the button is released.

RowfromXY doesnt calculate the correct row in the mousedown for some reason.

Catalina, Xojo 2018 R3

Can you provide a sample project? Perhaps we can take a look at how you’ve implemented it and maybe have some tips.

I can try.
You look at a project like this and think ‘Crikey. what do I pull out’

This is a sample project.
(Save the text with extension .xojo_xml_project )

To demonstrate the problem, run it.
click on row 14 and it is selected.

Then scroll down, and CTRL-click on row 70.
The popup menu appears, but the row doesnt highlight.
When the popup is cleared, row 70 isnt selected, row 14 isnt selected, but some other random row is.
I cannot work out what the ‘random’ row will be or why it isnt the original or the one I popped up from

<?xml version="1.0" encoding="UTF-8"?>
<RBProject version="2018r3" FormatVersion="2" MinIDEVersion="20070100">
<block type="Project" ID="0">
 <ProjectSavedInVers>2018.03</ProjectSavedInVers>
 <MajorVersion>1</MajorVersion>
 <MinorVersion>0</MinorVersion>
 <SubVersion>0</SubVersion>
 <Release>0</Release>
 <NonRelease>0</NonRelease>
 <Region></Region>
 <ShortVersion></ShortVersion>
 <LongVersion></LongVersion>
 <InfoVersion></InfoVersion>
 <AutoIncVersion>0</AutoIncVersion>
 <DefaultViewID>905385983</DefaultViewID>
 <ProjectType>0</ProjectType>
 <DefaultLanguage>0</DefaultLanguage>
 <CurrentLanguage>0</CurrentLanguage>
 <DefaultEncoding>0</DefaultEncoding>
 <BuildFlags>18688</BuildFlags>
 <UseBuildsFolder>1</UseBuildsFolder>
 <WebApp>0</WebApp>
 <Icon>
 </Icon>
 <MacCreator></MacCreator>
 <BuildCarbonMachOName>Listexample</BuildCarbonMachOName>
 <BundleIdentifier>com.ursasoftware.listexample</BundleIdentifier>
 <WinMDICaption></WinMDICaption>
 <BuildWinName>Listexample.exe</BuildWinName>
 <BuildWinMDI>0</BuildWinMDI>
 <BuildWinCompanyName></BuildWinCompanyName>
 <BuildWinProductName></BuildWinProductName>
 <BuildWinInternalName></BuildWinInternalName>
 <BuildWinFileDescription></BuildWinFileDescription>
 <UseGDIPlus>0</UseGDIPlus>
 <HiDPI>1</HiDPI>
 <DarkMode>1</DarkMode>
 <BuildLinuxX86Name>Listexample</BuildLinuxX86Name>
 <DebuggerCommandLine></DebuggerCommandLine>
 <LinuxArchitecture>1</LinuxArchitecture>
 <MacArchitecture>1</MacArchitecture>
 <OptimizationLevel>0</OptimizationLevel>
 <CopyWindowsRedist>0</CopyWindowsRedist>
 <WindowsVersions>{35138b9a-5d96-4fbd-8e2d-a2440225f93a}|{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}|{1f676c76-80e1-4239-95bb-83d0f6d0da78}|{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}</WindowsVersions>
 <WindowsRunAs>0</WindowsRunAs>
</block>
<block type="Module" ID="1335302143">
 <ObjName>App</ObjName>
 <ObjContainerID>0</ObjContainerID>
 <IsClass>1</IsClass>
 <Superclass>Application</Superclass>
 <ItemFlags>1</ItemFlags>
 <IsInterface>0</IsInterface>
 <IsApplicationObject>1</IsApplicationObject>
 <Compatibility></Compatibility>
 <PropertyVal Name="MenuBar">363689983</PropertyVal>
 <ViewBehavior>
 </ViewBehavior>
 <Constant>
  <ItemName>kFileQuitShortcut</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>1570246655</PartID>
  <TextEncoding>134217984</TextEncoding>
  <ItemType>0</ItemType>
  <ItemDef></ItemDef>
  <ItemFlags>0</ItemFlags>
  <ConstantInstance>
   <ItemPlatform>1</ItemPlatform>
   <ItemLanguage>0</ItemLanguage>
   <ItemDef>Cmd+Q</ItemDef>
  </ConstantInstance>
  <ConstantInstance>
   <ItemPlatform>5</ItemPlatform>
   <ItemLanguage>0</ItemLanguage>
   <ItemDef>Ctrl+Q</ItemDef>
  </ConstantInstance>
 </Constant>
 <Constant>
  <ItemName>kFileQuit</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>215705599</PartID>
  <TextEncoding>134217984</TextEncoding>
  <ItemType>0</ItemType>
  <ItemDef>&amp;Quit</ItemDef>
  <ItemFlags>0</ItemFlags>
  <ConstantInstance>
   <ItemPlatform>2</ItemPlatform>
   <ItemLanguage>0</ItemLanguage>
   <ItemDef>E&amp;xit</ItemDef>
  </ConstantInstance>
 </Constant>
 <Constant>
  <ItemName>kEditClear</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>225507327</PartID>
  <TextEncoding>134217984</TextEncoding>
  <ItemType>0</ItemType>
  <ItemDef>&amp;Delete</ItemDef>
  <ItemFlags>0</ItemFlags>
  <ConstantInstance>
   <ItemPlatform>2</ItemPlatform>
   <ItemLanguage>0</ItemLanguage>
   <ItemDef>&amp;Delete</ItemDef>
  </ConstantInstance>
  <ConstantInstance>
   <ItemPlatform>5</ItemPlatform>
   <ItemLanguage>0</ItemLanguage>
   <ItemDef>&amp;Delete</ItemDef>
  </ConstantInstance>
 </Constant>
</block>
<block type="Window" ID="905385983">
 <ObjName>Window1</ObjName>
 <ObjContainerID>0</ObjContainerID>
 <IsClass>1</IsClass>
 <Superclass>Window</Superclass>
 <ItemFlags>1</ItemFlags>
 <IsInterface>0</IsInterface>
 <Compatibility></Compatibility>
 <HookInstance>
  <ItemName>Open</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>1525110783</PartID>
  <ItemSource>
   <TextEncoding>134217984</TextEncoding>
   <SourceLine>Sub Open()</SourceLine>
   <SourceLine>Call fillList</SourceLine>
   <SourceLine>End Sub</SourceLine>
  </ItemSource>
 </HookInstance>
 <Method>
  <ItemName>FillList</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>864172031</PartID>
  <ItemSource>
   <TextEncoding>134217984</TextEncoding>
   <SourceLine>Sub FillList()</SourceLine>
   <SourceLine>listbox1.DeleteAllRows  'because some of the data may have been removed</SourceLine>
   <SourceLine></SourceLine>
   <SourceLine>for x as integer = 0 to 200</SourceLine>
   <SourceLine>listbox1.AddRow str(x)</SourceLine>
   <SourceLine>next</SourceLine>
   <SourceLine></SourceLine>
   <SourceLine>listbox1.listindex = currrow</SourceLine>
   <SourceLine></SourceLine>
   <SourceLine>End Sub</SourceLine>
  </ItemSource>
  <TextEncoding>134217984</TextEncoding>
  <AliasName></AliasName>
  <ItemFlags>0</ItemFlags>
  <IsShared>0</IsShared>
  <ItemParams></ItemParams>
  <ItemResult></ItemResult>
 </Method>
 <Property>
  <ItemName>currrow</ItemName>
  <Compatibility></Compatibility>
  <Visible>1</Visible>
  <PartID>1383897087</PartID>
  <ItemSource>
   <TextEncoding>134217984</TextEncoding>
   <SourceLine>currrow As Integer</SourceLine>
   <SourceLine></SourceLine>
  </ItemSource>
  <TextEncoding>134217984</TextEncoding>
  <ItemDeclaration>currrow As Integer</ItemDeclaration>
  <ItemFlags>0</ItemFlags>
  <IsShared>0</IsShared>
 </Property>
 <PropertyVal Name="MenuBarVisible">True</PropertyVal>
 <PropertyVal Name="MenuBar">363689983</PropertyVal>
 <PropertyVal Name="Backdrop">0</PropertyVal>
 <PropertyVal Name="BackColor">16777215</PropertyVal>
 <PropertyVal Name="HasBackColor">False</PropertyVal>
 <PropertyVal Name="FullScreen">False</PropertyVal>
 <PropertyVal Name="LiveResize">True</PropertyVal>
 <PropertyVal Name="Visible">True</PropertyVal>
 <PropertyVal Name="Placement">0</PropertyVal>
 <PropertyVal Name="ImplicitInstance">True</PropertyVal>
 <PropertyVal Name="MacProcID">0</PropertyVal>
 <PropertyVal Name="Composite">False</PropertyVal>
 <PropertyVal Name="FullScreenButton">False</PropertyVal>
 <PropertyVal Name="MinimizeButton">True</PropertyVal>
 <PropertyVal Name="MaximizeButton">True</PropertyVal>
 <PropertyVal Name="Resizeable">True</PropertyVal>
 <PropertyVal Name="CloseButton">True</PropertyVal>
 <PropertyVal Name="Title">Untitled</PropertyVal>
 <PropertyVal Name="Frame">0</PropertyVal>
 <PropertyVal Name="MaxHeight">32000</PropertyVal>
 <PropertyVal Name="MaxWidth">32000</PropertyVal>
 <PropertyVal Name="MinHeight">64</PropertyVal>
 <PropertyVal Name="MinWidth">64</PropertyVal>
 <PropertyVal Name="Height">400</PropertyVal>
 <PropertyVal Name="Width">600</PropertyVal>
 <ViewBehavior>
  <ViewProperty>
   <ObjName>Name</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>ID</PropertyGroup>
   <ItemType>String</ItemType>
   <EditorType>String</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Interfaces</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>ID</PropertyGroup>
   <ItemType>String</ItemType>
   <EditorType>String</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Super</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>ID</PropertyGroup>
   <ItemType>String</ItemType>
   <EditorType>String</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Width</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Size</PropertyGroup>
   <PropertyValue>600</PropertyValue>
   <ItemType>Integer</ItemType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Height</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Size</PropertyGroup>
   <PropertyValue>400</PropertyValue>
   <ItemType>Integer</ItemType>
  </ViewProperty>
 

  <ViewProperty>
   <ObjName>Frame</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Frame</PropertyGroup>
   <PropertyValue>0</PropertyValue>
   <ItemType>Integer</ItemType>
   <EditorType>Enum</EditorType>
   <Enumeration>
    <ItemDef>0 - Document</ItemDef>
    <ItemDef>1 - Movable Modal</ItemDef>
    <ItemDef>2 - Modal Dialog</ItemDef>
    <ItemDef>3 - Floating Window</ItemDef>
    <ItemDef>4 - Plain Box</ItemDef>
    <ItemDef>5 - Shadowed Box</ItemDef>
    <ItemDef>6 - Rounded Window</ItemDef>
    <ItemDef>7 - Global Floating Window</ItemDef>
    <ItemDef>8 - Sheet Window</ItemDef>
    <ItemDef>9 - Metal Window</ItemDef>
    <ItemDef>11 - Modeless Dialog</ItemDef>
   </Enumeration>
  </ViewProperty>


  <ViewProperty>
   <ObjName>MacProcID</ObjName>
   <PropertyGroup>OS X (Carbon)</PropertyGroup>
   <PropertyValue>0</PropertyValue>
   <ItemType>Integer</ItemType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>ImplicitInstance</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Behavior</PropertyGroup>
   <PropertyValue>True</PropertyValue>
   <ItemType>Boolean</ItemType>
   <EditorType>Boolean</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Placement</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Behavior</PropertyGroup>
   <PropertyValue>0</PropertyValue>
   <ItemType>Integer</ItemType>
   <EditorType>Enum</EditorType>
   <Enumeration>
    <ItemDef>0 - Default</ItemDef>
    <ItemDef>1 - Parent Window</ItemDef>
    <ItemDef>2 - Main Screen</ItemDef>
    <ItemDef>3 - Parent Window Screen</ItemDef>
    <ItemDef>4 - Stagger</ItemDef>
   </Enumeration>
  </ViewProperty>
  <ViewProperty>
   <ObjName>Visible</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Behavior</PropertyGroup>
   <PropertyValue>True</PropertyValue>
   <ItemType>Boolean</ItemType>
   <EditorType>Boolean</EditorType>
  </ViewProperty>


 
  <ViewProperty>
   <ObjName>MenuBar</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Menus</PropertyGroup>
   <ItemType>MenuBar</ItemType>
   <EditorType>MenuBar</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>MenuBarVisible</ObjName>
   <Visible>1</Visible>
   <PropertyGroup>Deprecated</PropertyGroup>
   <PropertyValue>True</PropertyValue>
   <ItemType>Boolean</ItemType>
   <EditorType>Boolean</EditorType>
  </ViewProperty>
  <ViewProperty>
   <ObjName>currrow</ObjName>
   <PropertyGroup>Behavior</PropertyGroup>
   <ItemType>Integer</ItemType>
  </ViewProperty>
 </ViewBehavior>
 <ControlBehavior>
  <Superclass>Listbox</Superclass>
  <HookInstance>
   <ItemName>Change</ItemName>
   <Compatibility></Compatibility>
   <Visible>1</Visible>
   <PartID>1209503743</PartID>
   <ItemSource>
    <TextEncoding>134217984</TextEncoding>
    <SourceLine>Sub Change()</SourceLine>
    <SourceLine>currrow = me.listindex</SourceLine>
    <SourceLine>End Sub</SourceLine>
   </ItemSource>
  </HookInstance>
  <HookInstance>
   <ItemName>MouseDown</ItemName>
   <Compatibility></Compatibility>
   <Visible>1</Visible>
   <PartID>1821911039</PartID>
   <ItemSource>
    <TextEncoding>134217984</TextEncoding>
    <SourceLine>Function MouseDown(x As Integer, y As Integer) As Boolean</SourceLine>
    <SourceLine>System.debugLog "Listindex in mousedown was " + str(me.listindex)</SourceLine>
    <SourceLine>system.debugLog "Row from xy was " + str(me.RowFromXY(x,y))</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>dim old as integer = currrow</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>if IsContextualClick then</SourceLine>
    <SourceLine>dim m1 as new menuitem</SourceLine>
    <SourceLine>m1.text = "dummy"</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>dim m2 as new menuitem</SourceLine>
    <SourceLine>m2.text = "dummy2"</SourceLine>
    <SourceLine>m1.append m2</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>dim r as menuitem</SourceLine>
    <SourceLine>r = m1.PopUp</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>FillList  'should fill the list and select correct row afterwards</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>end if</SourceLine>
    <SourceLine></SourceLine>
    <SourceLine></SourceLine>
    <SourceLine>End Function</SourceLine>
   </ItemSource>
  </HookInstance>
 </ControlBehavior>
 <Control>
  <ControlClass>Listbox</ControlClass>
  <ItemName>Listbox</ItemName>
  <PropertyVal Name="InitialParent"></PropertyVal>
  <PropertyVal Name="DataField"></PropertyVal>
  <PropertyVal Name="DataSource"></PropertyVal>
  <PropertyVal Name="AutoHideScrollbars">True</PropertyVal>
  <PropertyVal Name="ColumnsResizable">False</PropertyVal>
  <PropertyVal Name="SelectionType">0</PropertyVal>
  <PropertyVal Name="RequiresSelection">False</PropertyVal>
  <PropertyVal Name="Visible">True</PropertyVal>

  <PropertyVal Name="Height">360</PropertyVal>
  <PropertyVal Name="Width">332</PropertyVal>
  <PropertyVal Name="Top">20</PropertyVal>
  <PropertyVal Name="Left">33</PropertyVal>
  <PropertyVal Name="Scope">0</PropertyVal>
  <PropertyVal Name="Super">Listbox</PropertyVal>
  <PropertyVal Name="Index">-2147483648</PropertyVal>
  <PropertyVal Name="Name">Listbox1</PropertyVal>
  <ControlIndex>0</ControlIndex>
  <Locked>0</Locked>
  <PartID>960483327</PartID>
 </Control>
</block>
<block type="Menu" ID="363689983">
 <ObjName>MainMenuBar</ObjName>
 <ObjContainerID>0</ObjContainerID>
 <MenuItem>
  <ItemSpecialMenu>0</ItemSpecialMenu>
  <ItemName>FileMenu</ItemName>
  <ItemText>&amp;File</ItemText>
  <ItemIndex>-2147483648</ItemIndex>
  <ItemShortcut></ItemShortcut>
  <MenuAutoEnable>1</MenuAutoEnable>
  <TextEncoding>134217984</TextEncoding>
  <MenuItemVisible>1</MenuItemVisible>
  <ItemFlags>0</ItemFlags>
  <Superclass>MenuItem</Superclass>

 </MenuItem>
 <MenuItem>
  <ItemSpecialMenu>0</ItemSpecialMenu>
  <ItemName>EditMenu</ItemName>
  <ItemText>&amp;Edit</ItemText>
  <ItemIndex>-2147483648</ItemIndex>
  <ItemShortcut></ItemShortcut>
  <MenuAutoEnable>1</MenuAutoEnable>
  <TextEncoding>134217984</TextEncoding>
  <MenuItemVisible>1</MenuItemVisible>
  <ItemFlags>0</ItemFlags>
  <Superclass>MenuItem</Superclass>

 </MenuItem>
</block>
<block type="BuildAutomation" ID="682254335">
 <ObjName>Build Automation</ObjName>
 <ObjContainerID>0</ObjContainerID>
</block>
<block type="BuildStepsList" ID="2103255039">
 <ObjName>Linux</ObjName>
 <ObjContainerID>682254335</ObjContainerID>
</block>
<block type="BuildProjectStep" ID="2064953343">
 <ObjName>Build</ObjName>
 <ObjContainerID>2103255039</ObjContainerID>
</block>
<block type="BuildStepsList" ID="1884477439">
 <ObjName>Mac OS X</ObjName>
 <ObjContainerID>682254335</ObjContainerID>
</block>
<block type="BuildProjectStep" ID="1792555007">
 <ObjName>Build</ObjName>
 <ObjContainerID>1884477439</ObjContainerID>
</block>
<block type="BuildStepsList" ID="1749293055">
 <ObjName>Windows</ObjName>
 <ObjContainerID>682254335</ObjContainerID>
</block>
<block type="BuildProjectStep" ID="1035137023">
 <ObjName>Build</ObjName>
 <ObjContainerID>1749293055</ObjContainerID>
</block>
<block type="UIState" ID="0">
 <StudioWindowState>
  <EditBounds><Rect left="158" top="23" width="1374" height="827"/></EditBounds>
  <WindowMaximized>0</WindowMaximized>
  <Editors>
   <EditorCount>1</EditorCount>
   <Editor>
    <EditorIndex>0</EditorIndex>
    <EditorLocation>Window1.Controls.Listbox1.MouseDown</EditorLocation>
    <EditorPath></EditorPath>
    <EditingPartID>905385983</EditingPartID>
    <EditorPath></EditorPath>
   </Editor>
   <SelectedTab>0</SelectedTab>
  </Editors>
 </StudioWindowState>
</block>
</RBProject>

Are you aware of the ConstructContextualMenu and ContextualMenuAction events? I haven’t had issues using them.

I’ve modified your project to show how I would handle the contextual (Right-Click) menu.

Does that help? Or is there a reason you weren’t using those events?

1 Like

Promising.

Now to recode my legacy code to use this method and see what happens.
Thanks for looking at it!

Solved. Dont understand the difference, but I wont complain. :slight_smile: