• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Enable/disable my menu

I wish on some moment to disable all my menu items and after some time to enable.
How to do that?

Is easy to do a loop into menu and change the enable property, but I have (possible) some items already disabled.

How to know when re-enable the menu what menu's items was enabled and what was disabled before disable action? Sorry for this words game :)
0
ginsonic
Asked:
ginsonic
  • 3
  • 2
  • 2
  • +1
1 Solution
 
KnightyCommented:
save the states of the menu in an array before disable it
0
 
ginsonicAuthor Commented:
Codes please
0
 
kretzschmarCommented:
usual you must only disable the top-items,
for this just iterate through the main-menu-items

for i := 0 to mainmenu1.items.count-1 do
  mainmenu1.items[i].Enabled := YourSwitch;  //true or false

just from head, not tested

meikl ;-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ginsonicAuthor Commented:
I know that (read my question), but the problem is that I have couple top items already disabled, too.
0
 
kretzschmarCommented:
well ok, a sample

unit DisEn_Menu_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    N41: TMenuItem;
    N51: TMenuItem;
    N61: TMenuItem;
    N71: TMenuItem;
    N81: TMenuItem;
    N91: TMenuItem;
    N101: TMenuItem;
    N111: TMenuItem;
    N121: TMenuItem;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FMenuDisabled : Boolean;
    FDisabledMenus : TList;
    procedure DisableMenu(AItem : TMenuItem); OverLoad;
    procedure DisableMenu; OverLoad;
    procedure EnableMenu(AItem : TMenuItem); OverLoad;
    procedure EnableMenu; OverLoad;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FMenuDisabled := False;
  FDisabledMenus := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FDisabledMenus.Free;
end;

procedure TForm1.DisableMenu(AItem : TMenuItem);
var i : integer;
begin
  if not FMenuDisabled then
  begin
    for i := 0 to AItem.Count - 1 do
      DisableMenu(AItem.Items[i]);
    if not AItem.Enabled then
      FDisabledMenus.Add(AItem)
    else
      AItem.Enabled := False;
  end;
end;

procedure TForm1.DisableMenu;
var i : Integer;
begin
  if not FMenuDisabled then
  begin
    for i := 0 to MainMenu1.Items.Count - 1 do
      DisableMenu(MainMenu1.Items[i]);
    FMenuDisabled := True;
  end;
end;

procedure TForm1.EnableMenu(AItem : TMenuItem);
var i : integer;
begin
  if FMenuDisabled then
  begin
    for i := 0 to AItem.Count - 1 do
      EnableMenu(AItem.Items[i]);
    if FDisabledMenus.IndexOf(AItem) > -1 then
        //nothing, was disabled before
    else
      AItem.Enabled := true;
  end;
end;


procedure TForm1.EnableMenu;
var i : integer;
begin
  if FMenuDisabled then
  begin
    for i := 0 to MainMenu1.Items.Count - 1 do
      EnableMenu(MainMenu1.Items[i]);
    FMenuDisabled := False;
    FDisabledMenus.Clear;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Case FMenuDisabled of
    True  : EnableMenu;
    False : DisableMenu;
  end;
end;

end.


meikl ;-)
0
 
j42Commented:
// Usage:

// Disable:
// EnableDisable(MainMenu1, False);

// Enable:
// EnableDisable(MainMenu1, True);

unit m;

interface

uses
  Menus;

procedure EnableDisable(const menu: TMainMenu; const enable: boolean);

implementation

procedure EnableDisable(const menu: TMainMenu; const enable: boolean);
var
  i: integer;
begin
  if enable then
  begin
    for i := 0 to (menu.Items.Count - 1) do
    begin
      menu.Items[i].Enabled := boolean(menu.Items[i].Tag);
    end;
  end
  else
  begin
    for i := 0 to (menu.Items.Count - 1) do
    begin
      menu.Items[i].Tag := Ord(menu.Items[i].Enabled);
      menu.Items[i].Enabled := enable;
    end;
  end;
end;

end.
0
 
j42Commented:
Oops, little bug.

Never call enable before you disabled the menu. Otherwise all your menu items will be disabled.

Regards
0
 
ginsonicAuthor Commented:
Thanks for help :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now