Renaming TAction for another form

2019-06-25 08:42发布

问题:

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.

回答1:

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?



回答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.