?
Solved

Enable/disable my menu

Posted on 2003-03-26
8
Medium Priority
?
263 Views
Last Modified: 2010-04-04
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
Comment
Question by:ginsonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:Knighty
ID: 8212141
save the states of the menu in an array before disable it
0
 
LVL 9

Author Comment

by:ginsonic
ID: 8212159
Codes please
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8212388
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!

 
LVL 9

Author Comment

by:ginsonic
ID: 8215594
I know that (read my question), but the problem is that I have couple top items already disabled, too.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 8216056
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
 
LVL 2

Expert Comment

by:j42
ID: 8216417
// 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
 
LVL 2

Expert Comment

by:j42
ID: 8216471
Oops, little bug.

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

Regards
0
 
LVL 9

Author Comment

by:ginsonic
ID: 8217222
Thanks for help :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question