delphidelphi-10.4-sydney

Right-click doesn't update ItemIndex in TControlList


I've added a TPopupMenu to a TControlList, on right-click the ItemIndex isn't updated to reflect the item clicked on. Is there a way to make a right-click respond in a similar way to a left-click?

This would be nice so that the user right-clicks on a particular item, the PopupMenu is associated with that item, not the currently focused item.


Solution

  • The HotItemIndex property can be used to detect which item has been right-clicked by using the following

    procedure TListingList.clListingsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      inherited;
      if Button <> mbRight then
        Exit;
    
      if clListings.HotItemIndex <> -1 then
        clListings.ItemIndex := clListings.HotItemIndex;
    end;
    

    This mostly works, but if you already have a TPopupmenu visible already, then the HotItemIndex is -1. This means that consecutive right-clicks don't popup for the correct item - but I can live with this. I think a MousePosToItemIndex or ItemIndexUnderMouse method would be required to fix this properly.