1 (edited by thezimguy 2018-08-07 17:15:05)

Topic: [SOLVED] Dynamic Menu - Procedure/Function

Solved

var 
    MainMenu1 : TMenuItem;
Procedure CreateMenuItem (Const  Menu : TMenuItem ; Caption : String );
var MenuItem : TMenuItem;
Begin
    MenuItem := TMenuItem.Create(Form1.Menu);
    MenuItem.Caption := Caption;
    Form1.Menu.Items.Add(MenuItem);
End;

procedure Form1_OnShow (Sender: string; Action: string); // event OnShow
Begin
        CreateMenuItem(MainMenu1,'Users');//Create mainMenu
        CreateMenuItem(MainMenu1,'Pages');//Create mainMenu
        CreateMenuItem(MainMenu1,'Reports');//Create mainMenu
End;

begin

end.
@thezimguy

Re: [SOLVED] Dynamic Menu - Procedure/Function

Hello thezimguy

Please a look on the attachement
I use this way for my own menus and it works fine

JB

Post's attachments

Attachment icon MENUS TEST.rar 302.94 kb, 479 downloads since 2018-08-05 

Re: [SOLVED] Dynamic Menu - Procedure/Function

Thanks jean.brezhonek
I have check the example but what I wanted was to create a function which I can call to create the menu.

I am able to create the menu as you have done but I want it to be simplified by using a function which I can call to create the menu.

Thank you once again

@thezimguy

Re: [SOLVED] Dynamic Menu - Procedure/Function

Hello again

What error message do you get ?

JB

5 (edited by thezimguy 2018-08-05 23:21:53)

Re: [SOLVED] Dynamic Menu - Procedure/Function

Solved

This is what I was looking for

var 
   MainMenu1 : TMenuItem;

Procedure CreateMenuItem (Const  Menu : TMenuItem ; Caption : String );
var MenuItem : TMenuItem;
Begin
    MenuItem := TMenuItem.Create(Form1.Menu);
    MenuItem.Caption := Caption;
    Form1.Menu.Items.Add(MenuItem);
End;

procedure Form1_OnShow (Sender: string; Action: string); // event OnShow
Begin
        CreateMenuItem(MainMenu1,'Files');//Create mainMenu
End;

begin

end.
@thezimguy

Re: [SOLVED] Dynamic Menu - Procedure/Function

Hello thezimguy

I've tried your code below.

For better visibility I disabled the native menus of MVD to reveal only those of the test.

It works fine, without any error message.

What does not suit you in your code ?

As is, this routine is less restrictive than the code used previously to create my own menus and it works

JB

Post's attachments

Attachment icon Menus_Test.rar 292.23 kb, 475 downloads since 2018-08-06 

7 (edited by thezimguy 2018-08-06 08:50:03)

Re: [SOLVED] Dynamic Menu - Procedure/Function

There is no problem with creating the Menu items from a function or procedure.
THE NEW TOPIC IS HOW TO ADD A DYNAMIC OnClick Event to the procedure from a string


var 
   MainMenu1 : TMenuItem;

Procedure CreateMenuItem (Const  Menu : TMenuItem ; Caption : String; DynamicEvent );
var MenuItem : TMenuItem;
Begin
    MenuItem := TMenuItem.Create(Form1.Menu);
    MenuItem.Caption := Caption;
    MenuItem.OnClick := @DynamicEvent; /////////////////I want to add dynamic event here from string
    Form1.Menu.Items.Add(MenuItem);
End;

procedure Form1_OnShow (Sender: string; Action: string); // event OnShow
Begin
        CreateMenuItem(MainMenu1,'Users');//Create mainMenu
End;

begin

end.

Eg. if I call the procedure

CreateMenuItem(MainMenu1,'Users',Event1_onClick);

i want it to create

  • a menu item 'Users'

  • an onClick event on Event1_onClick

@thezimguy

Re: [SOLVED] Dynamic Menu - Procedure/Function

You can't add menu item with dynamic event, but you can use one event for several items like this:

procedure DynamicEvent (Sender: TObject);
begin
     if TMenuItem(Sender).Caption = '&Item1' then
     begin
         // your action
     end;

     if TMenuItem(Sender).Caption = '&Item2' then
     begin
         // your action
     end;
end;
Dmitry.

9 (edited by thezimguy 2018-08-07 17:36:51)

Re: [SOLVED] Dynamic Menu - Procedure/Function

Wow
You are great Dmitry
You have really made things easy for database system making

It worked perfectly. My dynamic menu is fully ready with submenus and images.
Thanks

I LOVE MVD

@thezimguy