Recent

Author Topic: show popupmenu in listview if  (Read 380 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 367
    • ericksystem software
show popupmenu in listview if
« on: February 12, 2025, 01:39:03 am »
Hi friends, I have a listview with a popupmenu and I want the popupmenu to only be displayed when I right-click on an existing item. I have used the following code but it shows an error:  :(
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1Popup(Sender: TObject);
  2. begin
  3.   if ListView1.SelCount=0 then begin
  4.     Abort;
  5.   end;
  6. end;
  7.  

I tried this other one but the popupmenu always appears even if I don't select anything.  :(
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1Popup(Sender: TObject);
  2. begin
  3.   if ListView1.Selected = nil then
  4.     Exit;
  5. end;

help please
I love desktop software
https://www.ericksystem.com

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1485
    • Lebeau Software
Re: show popupmenu in listview if
« Reply #1 on: February 12, 2025, 02:52:04 am »
Rather than using the TListView.Selected property, try instead the TListView.GetItemAt() method, using the TPopupMenu.PopupPoint as the coordinates to search at.

However, I would not abort the PopupMenu in its own OnPopup event.  Instead, I would set TPopupMenu.AutoPopup to false, and then call TPopupMenu.Popup() in the TListView.OnMouseUp or TListView.OnContextPopup event if TListView.GetItemAt() finds an item under the mouse.

Do be aware that the popup menu can be invoked by keyboard instead of mouse, in which case using TListView.Selected will make sense in that case.
« Last Edit: February 12, 2025, 02:54:13 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: show popupmenu in listview if
« Reply #2 on: February 12, 2025, 06:19:40 am »
Hi friends, I have a listview with a popupmenu and I want the popupmenu to only be displayed when I right-click on an existing item.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
  2. begin
  3.   if Selected then
  4.     ListView1.PopupMenu := PopupMenu1
  5.   else
  6.     ListView1.PopupMenu := nil
  7. end;
At start do not set ListView1.PopupMenu

 

TinyPortal © 2005-2018