使用菜單項沒有更新其禁用狀態:德爾福XE2,32位Windows VCL窗體應用程序ActionMainMenuBar後手動添加行動項目
在ActionManager,我已經添加了一個動作,並指定類別名稱。然後將ActionManager中的類別拖放到表單上的ActionMainMenuBar中。我這樣做了在ActionMainMenuBar上創建菜單項。因爲我打算通過代碼手動創建和添加動作,並且沒有真正用於「第一」動作,所以我通過設置Visbile := False
來隱藏它。
但是,在運行時,即使在編程創建操作並將它們添加到菜單後,菜單仍處於禁用狀態 - 這是不期望的,因爲添加的操作已啓用且具有有效的OnExecute事件處理程序。
我的問題是如何刷新菜單項,以便在添加一個或多個操作項(子菜單項)後啓用它?
在代碼中,我有這樣的:
// 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;
這裏是ActionMainMenuBar,所述ActionManager和初始動作(其具有可見設置爲false)的屏幕捕獲,在設計模式:
TIA。