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

avoid click event

Hi,
 I need to override a click event from happening..
When i click a menu item, it shld check for a flag
and then popup the submenus based on the flag..
For a key events, i can set the key to #0,
How to achieve this for click event?..I'm using Tdxbarsubmenuitem...

Thanks
Prema
0
premaa
Asked:
premaa
2 Solutions
 
User137Commented:
Click-events have one parameter: TObject Sender.

If i understood your question, you call this Click procedure from elsewhere? If so, you can call for example:

MenuItemClick(nil);

and

MenuItemClick(Sender: TObject);
begin
  if Sender=nil then begin
    // What happens when called from outside

  end else begin
    // What happens when clicked normally

  end;
end;
0
 
j42Commented:
Hi,

I'm not sure if I understood your question? You want to check the flag before the menu is expanded and then decide wich items are in the menu?

Example:

If flag is true
File
  Open
  Close

If flag is false
File
  Foo

In the OnClick event of the File menu item code is to be:

OpenItem.Visible := flag;
CloseItem.Visible := flag;
FooItem.Visible := not flag;



I hope this may help. Please apologize my cryptic notation!

Regards
J
0
 
j42Commented:
// Some code

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    Close1: TMenuItem;
    Foo1: TMenuItem;
    CheckBox1: TCheckBox;
    procedure File1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.File1Click(Sender: TObject);
begin
  Open1.Visible := CheckBox1.Checked;
  Close1.Visible := CheckBox1.Checked;
  Foo1.Visible := not CheckBox1.Checked;
end;

end.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DragonSlayerCommented:
handle the OnPopup event instead of OnClick
0
 
premaaAuthor Commented:
Hi
user137,
Will the pop up not happen then? i tried to
make the sender nil in the barmanager click evnt
that raised an AV and then poped up the menu..

j42,
 I don't want to hide / disable the menus..as
there are many..just want to know if it's possible
using standard events alone..

Dragon ,
 what shld i do with the onpopup??
i tried to set it to nil, didn't work
the help says, a method "Do click" is there
and it shld be overriden..
I've created a bar manager that has many menu
and submenu items at design time itself..
how to override this procedure declared as
dynamic??

Thanks to all
Prema


 
0
 
User137Commented:
"make sender nil" ???

No, you can't treat Objects like that...

For this case you can set the parameter to be any TObject on your project. So That Sender can contain a big variety of Flags.

Call examples:
MenuItemClick(nil);
MenuItemClick(MenuItem1);

OnClickEvent example:
SubMenuItem1Click(Sender: TObject);
begin
 if Sender=nil then begin
   ShowMessage('Flag is empty');
 end else if sender=MenuItem1 then begin
   ShowMessage('OnClick-Event called by MenuItem1');
 end else begin
   ShowMessage('Flag is non zero');
 end;
end;
0
 
j42Commented:
DragonSlayer,
> handle the OnPopup event instead of OnClick
Are you talking about a TPopupMenu?

premaa,
What is a 'Tdxbarsubmenuitem'?

Some additionl information to what User137 already explained (so I do not want to be graded for that):

- In Delphi the visual components are pointers. Usually Delphi takes care of them and they point to the memory they should. If you set one of them to Nil you are no longer able to use it and your application may crash.

- If every component has its own OnClick procedure (e.g. Button1Click(Sender: TObject), Button2Click(Sender: TObject) ...) you do not have to care about the Sender. Sometimes you want to handle all the events within one procedure AllButtonsClick(Sender: TObject). So you have to care about the button from which this procedure is called:

procedure AllButtonsClick(Sender: TObject);
begin
  if (Button1 = Sender) then
  begin
    // Do something related to Button1
  end

  else
  if (Button2 = Sender) then
  begin
    // Do something related to Button2
  end
end;

Of course you can call this procedure like any other procedure and pass any pointer (including Nil) as parameter. These calls are valid:
  AllButtonsClick(Button1);   // Button1 is a pointer!
  AllButtonsClick(Nil);
  AllButtonsClick($FFFFFFFF);
But not all of these calls make sense.

The only way I know to prevent a menu item from poping up is to keep it empty. So my new solution to your problem is:
- Create a top level menu structure like this
  File(empty), Edit(empty)
- For each create as many TPopup menus as needed (depends on your flag)
  PopupMenu1, 2 for File item ; PopupMenu3, 4 for Edit item;
- In the OnClick event of your top level menu item popup the proper TPopupMenu
In the example below I was to lazy to get the coordinates. I just defined consts. Maybe you can post another q: 'How to get the lower left corner of a TMenuItem?' ;-)

// ***** Begin code

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Edit1: TMenuItem;

    PopupMenu1: TPopupMenu;
    Test11: TMenuItem;

    PopupMenu2: TPopupMenu;
    Test21: TMenuItem;

    PopupMenu3: TPopupMenu;
    Test31: TMenuItem;

    PopupMenu4: TPopupMenu;
    Test41: TMenuItem;

    CheckBox1: TCheckBox;
    procedure AllMenusClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.AllMenusClick(Sender: TObject);
const
  DELTA_X_FILE = 6;
  DELTA_X_EDIT = 36;
  DELTA_Y = 40;
begin
  if (Sender = File1) then
  begin
    if CheckBox1.Checked then
    begin
      PopupMenu1.Popup(Self.Left + DELTA_X_FILE, Self.Top + DELTA_Y);
    end
    else
    begin
      PopupMenu2.Popup(Self.Left + DELTA_X_FILE, Self.Top + DELTA_Y);
    end;
  end
  else

  if (Sender = Edit1) then
  begin
    if CheckBox1.Checked then
    begin
      PopupMenu3.Popup(Self.Left + DELTA_X_EDIT, Self.Top + DELTA_Y);
    end
    else
    begin
      PopupMenu4.Popup(Self.Left + DELTA_X_EDIT, Self.Top + DELTA_Y);
    end;
  end;
end;

end.

// ***** End code

Regards
J
0
 
CleanupPingCommented:
premaa:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
kacorretiredCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to split points between j42 and User137
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now