0

I'm replacing my good old TMainMenu with TActionMainMenuBar but have some issues with it.

I'm currently using for my main menu items the 'OnClick' handler, but I can't find a way to reproduce the same behavior with TActionMainMenuBar.

For example, I create a TAction "Test" with category "Sample" and I drag/drop this on the TActionMainMenuBar. I can add code to the execute handler of the sub-menu "Test" because it has a TAction assigned to it, but I can't add event code to the main menu item "Sample" because it is just a Category / TActionClientItem of "Test" with no events.

I tried to assign an Action to this TActionClientItem, but Delphi XE3 is saying "You cannot set property ..." and when I click that it gives me a dialog "Actions not implemented for the current framework 'None'".

Another way would be two TActions, "Sample" and "Test" with "(no category)", but I can't drag/drop "Test" as sub-menu of "Sample" which I dropped on the TActionMenuBar before. It looks like I can only drag/drop main menu items, and not sub-menu items on an empty main menu item.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Gunter
  • 3
  • 3
  • Welcome to StackOverflow. Please take the time to properly format your question, and break it into paragraphs instead of posting a huge wall of text. If we can't read your question, it's very difficult to answer it. You can preview your post as you're writing it real-time in a WYSIWYG fashion by looking below the text area you're typing it into, so you can see how it will look to us when posted. For help formatting, click the orange `?` button above the top right corner of the text area. Thanks. – Ken White Sep 06 '13 at 23:39

1 Answers1

0

The purpose for using the main menu item's OnClick handler is usually to determine whether sub-menu items are enabled/disabled or visible. It's also usually where you enable/disable things like toolbar buttons that perform the same function:

procedure TForm1.MyMainMenuItemClick(Sender: TObject);
begin
  SomeMenuItem.Enabled := SomeConditionTest;
  SomeToolButton.Enabled := SomeMenuItem.Enabled;
  AnotherMenuItem.Enabled := AnotherConditionTest;
  AnotherToolButton.Enabled := AnotherMenuItem.Enabled;
end;

For TActionMainMenuBar items, you do this in the individual actions instead, in the OnUpdate event. The advantage of this is that when you enable/disable the action, all controls connected to the action are also enabled/disabled at the same time.

procedure TForm1.SomeActionUpdate(Sender: TObject);
begin
  SomeAction.Enabled := SomeConditionTest;  // Also controls the toolbutton
end;

procedure TForm1.AnotherActionUpdate(Sender: TObject);
begin
  AnotherAction.Enabled := AnotherConditionTest; // Toolbutton too.
end;

The OnUpdate event is called just before the the child items are displayed, which is the same time that the old main menu item's OnClick would be called.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Indeed, I use the OnClick handler for enabling or check marking (sub)menu items, or creating sub-menus on the fly which are dynamic. Your solution works perfect for what I need, but the advantage of the "old" OnClick method is the fact that all the stuff concerning that particular menu is all grouped together. – Gunter Sep 07 '13 at 06:47
  • @Gunter: But the bigger advantage of the "new" Action functionality is that **all stuff connected to the action** is grouped together. Menu items, popup menu items, tool buttons, and everything, and you don't have to update them separately - you just handle the action itself, and it *automatically updates everything else to the same value*. If you assign an image to an action, the same image appears on the menu item and the toolbar button. It's much more useful than the old way, once you get used to it. – Ken White Sep 07 '13 at 12:58
  • @ken Old topic but just to interject because I've just been having the same problem. You can't always do this in actions, for instance an edit menu may have cut/copy/paste items that are dependant on the type of object being displayed. It may be a text editor, it may be a chart, it may be a schematic. So the onclick event needs to check what type of window is currently active and interrogate that window whether something is selected (to enable the copy menu) etc and those objects may be different for each active window/object type. Much easier to do when the menu drops down. – Andy k Nov 03 '15 at 15:17