1 (edited by SandBird 2025-02-01 03:20:28)

Topic: Grid Custom Popup Menu, Right Click and Selection Problem

Hello,

I have been testing out this software and so far it is brilliant!

However, I have encountered a problem that I am unsure how to solve and would like to know if there is any solution, perhaps I am doing something wrong? (I am new to Pascal).

In summary: I think when you right-click on a grid row, the SelectedCount gets updated after the actions of OnMouseUp/OnMouseDown  which means one can't utilize the count for changing items in the menu.

A little bit in depth: I have a grid with some rows and a custom popup menu. The menu is defined as a separate procedure and gets to run every time there is either OnChange and OnMouseUp/OnMouseDown event.

Something like (simplified):

PopupMenuItem := TMenuItem.Create (PopupMenu);
PopupMenuItem .Caption := 'Always Visible';
PopupMenuItem .OnClick := @proc1;
PopupMenu.Items.Add(PopupMenuItem);

if (form.grid.SelectedCount > 0) and (form.grid.RowCount > 0) then
begin
  PopupMenuItem := TMenuItem.Create (PopupMenu);
  PopupMenuItem .Caption := 'Remove Selected';
  PopupMenuItem .OnClick := @proc2;
  PopupMenu.Items.Add(PopupMenuItem);
end;

if form.grid.RowCount > 0 then
begin
  PopupMenuItem := TMenuItem.Create (PopupMenu);
  PopupMenuItem .Caption := 'Remove All';
  PopupMenuItem .OnClick := @proc3;
  PopupMenu.Items.Add(PopupMenuItem );

The problem: upon first load of the GUI when nothing is selected in the grid, if you right-click on one of the rows, the menu that shows will not show "Remove Selected", only upon a second mouse click it will show it. As I explained, with my tests I think the SelectedCount is updated after the actions of actions of OnMouseUp/OnMouseDown.

I also tried the OnClick/OnCellClick events but from what I tested they only get triggered upon a left-click and not right-click.

Is there anyway to solve this? perhaps a better approach to all of this "custom popup menu" since one must also consider keyboard input etc.?

Any input would be much appreciate - thank you in advance!

SandBird

Re: Grid Custom Popup Menu, Right Click and Selection Problem

Hi


The function of deleting a row with a mouse can be enabled in the table properties.
But if you want to create additional conditional deletion items, you will have to manage the process manually.
You do not need to use your procedure in all table events.
You can try the following:
1. In the OnShow event of the main form, disable all items of the current PopUp menu of the table (Visible false).
   Disabling all items prevents the program from automatically showing the menu by right-clicking.
2. Create your own menu items or modify existing ones with the visibility of menu items turned off. Create events.
3. In the OnMouseUp event, analyze the right-click, the number of selected rows. Enable the visibility of the menu item/items.
   Manually call the PopUp display by coordinates. Turn off the visibility of the menu item.


For the operation of deleting a row or all rows, you can do with one menu item. In point 3, before the visibility of the menu item,
change the name of the menu item. Analysis and execution of code for different situations is done in the event that is assigned
to this menu item.

Re: Grid Custom Popup Menu, Right Click and Selection Problem

Thank you sparrow for your prompt reply and help!

Per your recommendation, I ended up hiding all of the current menu items (oddly that := nil didn't work for me, but I used the .Visible) and created a new menu which I triggered to show on right click using GetCursorPos and .Popup.

Just to note that the problem I was facing when I initially created this post still exists - using the existing context menu wasn't an option. The existing menu triggers before the OnMouseUp event of the grid which means anything I place to change the existing menu - Enable, Visible etc. - will only run after the menu shows (essentially the update will always be in on click delay).

But! as long as there is another way to achieve it, it is great!

Thank you again!

Re: Grid Custom Popup Menu, Right Click and Selection Problem

I hoped that everything would work out and so it did. ) This was checked.
The menu item can be: made invisible, turned off, deleted, inserted, added, etc. Without nil. I hope you paid attention to the menu on the right with classes. There are properties and methods for classes described there.
Everything is correct with GetCursorPos and .Popup.
Between Down and Up, the program calls the menu, paints the line, etc., simultaneously interrupting the process of executing other functions. This is not a problem, this is the program's work. The problem is sometimes how to get around it.