33 Votes

Delphi/Lazarus: ListView - Show PopupMenu only if an item is selected

Tip by Delphian | 2013-05-19 at 21:35

Starting Point: We have a ListView with a TPopUpMenu, which is providing some options for the items in the ListView (for example options for deleting an entry or something like that).

Problem: The popup menu is also shown, if there are no items selected in the ListView. For example when clicking on the empty area below the items. Now, we want to achieve that the popup will only be shown, if there are items selected in the ListView.

Solution: As a solution, we are using the following code written to the event OnPopup of the popup menu.

procedure TForm1.PopupMenu1Popup(Sender: TObject);
var
   item: TListItem;
begin
   item := ListView1.selected;
 
   if item = nil then abort;
end;

Here, we are setting the variable "item" to the selected item of our ListView. If no entry is selected, "item" is nil and we can cancel the showing of the popup menu with "abort". So, the popup menu will only be shown if there is an item selected.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

The Secure Password

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.