4

I have 2 forms which I'd like to share one single TActionManager and assigned TAction-s.

First form is main form - it holds TActionManager, TAction with caption "Action".

Main menu of Form1 has this action and menuitem caption property set to "Action A". Form2 includes Form1 and also assigns action to menuitem and caption is set to "Action B".

During design time everything looks good - menu items are named "Action A" and "Action B" in Form1 and Form2 and same action is assigned. It also works fine during runtime (OnExecute is properly processed for both forms).

However - during runtime, Form1 menu item has caption "Action A" and Form2 has menu item caption "Action" just like the action caption.

Am I including them incorrectly, is it possible to include Form1 on Form2 to share action and to change caption? Is it a bug in D2010 / CB2010?

Example:

  1. Create Form1 and Form2 and add main menu to both
  2. Drop TActionManager to Form1 and create Action1 in it.
  3. Assign menu item in Form1 Action1 and after that modify Caption (caption is shown now as bold indicating change from default value)
  4. Assign menu item in Form2 also Action1 (from Form1 TActionManager) and also change default caption.
  5. During runtime now Form1 has changed caption (different from Action1.Caption), and Form2 has identical caption to Action1.Caption although the caption should be new value as changed in step 4.
Coder12345
  • 3,431
  • 3
  • 33
  • 73
  • 1
    In Delphi 2009 this seems to work fine. If you change the `TAction.Caption`, menu item captions on both of my testing forms has been changed. – TLama May 01 '12 at 20:16
  • 1
    @TLama Only when the captions of the menu items aren't changed before (and after setting the Action property of course). – NGLN May 01 '12 at 21:04
  • @NGLN, forgot to mention that. Thanks! – TLama May 01 '12 at 21:08
  • I do not want to change TAction.Caption - that would change captions on both menus. Menus need to have different Caption. I added a few lines of code to explain above. – Coder12345 May 01 '12 at 23:54

2 Answers2

5

I reproduced your issue and indeed, this is strange behaviour. Besides the obvious question why to change the menu items captions, the following should work:

Set both menu item's Action property to the same action. Now the captions of the menu items are linked to that of the action and note that they are not stored any longer (they appear not bold in the object inspector). Subsequently, change the captions from the menu items to independent values, and note that they now are stored and that the menu items indeed change accordingly at designtime.

But at runtime, the caption of the second menuitem indeed follows that of the action again. This should not happen in my opinion.

What I did not reproduce is the inabbility to change it at runtime. The following works as expected (D7 here):

procedure TForm2.FormCreate(Sender: TObject);
begin
  mnuAction1.Caption := 'Action B';
end;

Sure you are changing the caption of the menu items instead of that of the action?

NGLN
  • 43,011
  • 8
  • 105
  • 200
2

The TAction.Caption overrides the TMenuItem.Caption when the action is linked to the menu item or when the action caption is changed. If you change the menu item caption in forms Loaded method, you should be able to have different captions for both forms.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • 1
    I do not agree. Changed menu item captions after setting the Action property should remain changed. – NGLN May 01 '12 at 21:16
  • And they do for Form1, but not Form2. They are also shown in bold text in IDE like you said in answer below indicating the value is changed from default value (default in this case being Action1.Caption). So this is probably a bug in Delphi/C++Builder. Haven't tested this in XE/XE2 is this still the case though. – Coder12345 May 02 '12 at 00:15