1
votes

Using: Delphi XE2, 32-bit Windows VCL forms application

In an ActionManager, I've added an action and assigned the category name. Then I drag and drop the category from the ActionManager onto the ActionMainMenuBar on the form. I did this to create the menu item on the ActionMainMenuBar. Because I plan to create and add actions manually via code, and have no real use for the 'first' action I hide it by setting Visbile := False.

However at runtime, even after programatically creating actions and adding them to the menu, the menu remains disabled - which is not expected, because the actions that are added are enabled and have a valid OnExecute event handler.

My question is how can I refresh the menu item so that its enabled after adding one or more action items (sub menu items)?

In code, I have this:

  // Create menu for each session in the Session menu   
  // eg. Session 1, Session 2, Session 3 etc.   
  var
    p: Integer;
    s: String;   
  begin
    // this code executes a number of times ie in a loop
    p := Pos(' ', s);
    a := TAction.Create(actMgr);
    a.Category := 'Session';
    a.Name := 'actSession' + Copy(s, p + 1, Length(s) - p);
    a.Caption := 'Session ' + Copy(s, p + 1, Length(s) - p);
    a.Enabled := True;
    a.OnExecute := actSessionExecute;

    p := ActionMainMenuBar1.ActionClient.Items[3].Items.Count - 1;
    actMgr.AddAction(a, ActionMainMenuBar1.ActionClient.Items[3].Items[p]);

  end;


  procedure TfMain.actSessionExecute(Sender: TObject);   
  begin
     showmessage(TAction(Sender).Name);   
  end;

Here is a screen capture of the ActionMainMenuBar, the ActionManager and the initial Action (which has Visible set to False), in design mode:

This is what it looks like initially in design mode

TIA.

1

1 Answers

0
votes

Calling ActionMainMenuBar1.ActionClient.Items[3].CommandStyle := csMenu; after adding your your action items will force the Commandproperties to be recreated, so your session categorie will be accessible.