Runtime created menu

[b][red]This message was edited by sziszi81 at 2006-9-18 22:20:46[/red][/b][hr]
Hi. I have a problem with creating a menu runtime.
I have the menu structure stored in a data table MENU(ITEM, Caption, Parent)

like:

FILE, &File,
FILENEW, &New, FILE
FILEOPEN, &Open, FILE
EDIT, &Edit,
EDITCOPY, &Copy, EDIT
etc.

i have a procedure that creates the menu for me, but it fails creating the submenus. Since i have to find the node where i attach the submenu by it's name (the Parent field in the table) i tried to do the following:

[code]
procedure...

var mi: TMenuItem;
begin
mi:=TMenuItem.Create(mm); && mm is the TMainMenu
mi.Caption:=Menu['Caption'];
mi.Name:=Menu['ITEM'];
if Menu.FieldByName('Parent').IsNull then
mm.Items.Add(mi)
else
(FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);
end;
[/code]


this line fails:
[b](FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);[/b]

What's wrong with the casting?
Or do you know a better way for this?



Thanks a lot,
Sziszi


Comments

  • : [b][red]This message was edited by sziszi81 at 2006-9-18 22:20:46[/red][/b][hr]
    : Hi. I have a problem with creating a menu runtime.
    : I have the menu structure stored in a data table MENU(ITEM, Caption, Parent)
    :
    : like:
    :
    : FILE, &File,
    : FILENEW, &New, FILE
    : FILEOPEN, &Open, FILE
    : EDIT, &Edit,
    : EDITCOPY, &Copy, EDIT
    : etc.
    :
    : i have a procedure that creates the menu for me, but it fails creating the submenus. Since i have to find the node where i attach the submenu by it's name (the Parent field in the table) i tried to do the following:
    :
    : [code]
    : procedure...
    :
    : var mi: TMenuItem;
    : begin
    : mi:=TMenuItem.Create(mm); && mm is the TMainMenu
    : mi.Caption:=Menu['Caption'];
    : mi.Name:=Menu['ITEM'];
    : if Menu.FieldByName('Parent').IsNull then
    : mm.Items.Add(mi)
    : else
    : (FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);
    : end;
    : [/code]
    :
    :
    : this line fails:
    : [b](FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);[/b]
    :
    : What's wrong with the casting?
    : Or do you know a better way for this?
    :
    :
    :
    : Thanks a lot,
    : Sziszi
    :
    :
    :
    Perhaps the Menu['PARENT'] component doesn't exist yest, because the Menu database isn't enumarated from top to bottom, but in another order. This could mean that the submenu is created before the mainmenu item.
  • : : [b][red]This message was edited by sziszi81 at 2006-9-18 22:20:46[/red][/b][hr]
    : : Hi. I have a problem with creating a menu runtime.
    : : I have the menu structure stored in a data table MENU(ITEM, Caption, Parent)
    : :
    : : like:
    : :
    : : FILE, &File,
    : : FILENEW, &New, FILE
    : : FILEOPEN, &Open, FILE
    : : EDIT, &Edit,
    : : EDITCOPY, &Copy, EDIT
    : : etc.
    : :
    : : i have a procedure that creates the menu for me, but it fails creating the submenus. Since i have to find the node where i attach the submenu by it's name (the Parent field in the table) i tried to do the following:
    : :
    : : [code]
    : : procedure...
    : :
    : : var mi: TMenuItem;
    : : begin
    : : mi:=TMenuItem.Create(mm); && mm is the TMainMenu
    : : mi.Caption:=Menu['Caption'];
    : : mi.Name:=Menu['ITEM'];
    : : if Menu.FieldByName('Parent').IsNull then
    : : mm.Items.Add(mi)
    : : else
    : : (FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);
    : : end;
    : : [/code]
    : :
    : :
    : : this line fails:
    : : [b](FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);[/b]
    : :
    : : What's wrong with the casting?
    : : Or do you know a better way for this?
    : :
    : :
    : :
    : : Thanks a lot,
    : : Sziszi
    : :
    : :
    : :
    : Perhaps the Menu['PARENT'] component doesn't exist yest, because the Menu database isn't enumarated from top to bottom, but in another order. This could mean that the submenu is created before the mainmenu item.
    :



    No it's not, because i use a (PARENT,ITEM) Index and i followed in the debugger and the menus were created before the submenus.


    i also tried simply creating in the designer a menu like this:

    AAA BBB
    AA

    and when i give
    [code]
    mi:=TMenuItem.Create(mm);
    mi.Caption:='A';
    AA1.Add(mi);
    [/code]
    this works fine,

    but if i try like:
    [code]
    mi:=TMenuItem.Create(mm);
    mi.Caption:='A';
    (FindComponent('AA1') as TMenuItem).Add(mi);
    [/code]

    this fails.
  • : : : [b][red]This message was edited by sziszi81 at 2006-9-18 22:20:46[/red][/b][hr]
    : : : Hi. I have a problem with creating a menu runtime.
    : : : I have the menu structure stored in a data table MENU(ITEM, Caption, Parent)
    : : :
    : : : like:
    : : :
    : : : FILE, &File,
    : : : FILENEW, &New, FILE
    : : : FILEOPEN, &Open, FILE
    : : : EDIT, &Edit,
    : : : EDITCOPY, &Copy, EDIT
    : : : etc.
    : : :
    : : : i have a procedure that creates the menu for me, but it fails creating the submenus. Since i have to find the node where i attach the submenu by it's name (the Parent field in the table) i tried to do the following:
    : : :
    : : : [code]
    : : : procedure...
    : : :
    : : : var mi: TMenuItem;
    : : : begin
    : : : mi:=TMenuItem.Create(mm); && mm is the TMainMenu
    : : : mi.Caption:=Menu['Caption'];
    : : : mi.Name:=Menu['ITEM'];
    : : : if Menu.FieldByName('Parent').IsNull then
    : : : mm.Items.Add(mi)
    : : : else
    : : : (FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);
    : : : end;
    : : : [/code]
    : : :
    : : :
    : : : this line fails:
    : : : [b](FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);[/b]
    : : :
    : : : What's wrong with the casting?
    : : : Or do you know a better way for this?
    : : :
    : : :
    : : :
    : : : Thanks a lot,
    : : : Sziszi
    : : :
    : : :
    : : :
    : : Perhaps the Menu['PARENT'] component doesn't exist yest, because the Menu database isn't enumarated from top to bottom, but in another order. This could mean that the submenu is created before the mainmenu item.
    : :
    :
    :
    :
    : No it's not, because i use a (PARENT,ITEM) Index and i followed in the debugger and the menus were created before the submenus.
    :
    :
    : i also tried simply creating in the designer a menu like this:
    :
    : AAA BBB
    : AA
    :
    : and when i give
    : [code]
    : mi:=TMenuItem.Create(mm);
    : mi.Caption:='A';
    : AA1.Add(mi);
    : [/code]
    : this works fine,
    :
    : but if i try like:
    : [code]
    : mi:=TMenuItem.Create(mm);
    : mi.Caption:='A';
    : (FindComponent('AA1') as TMenuItem).Add(mi);
    : [/code]
    :
    : this fails.
    :
    I was not being too bright. FindComponent() loops through the components of the form, not of the menu. All components should have their owner set to the form they are in. The code should be:
    [code]
    mi := TMenuItem.Create(Self); // Self is the form
    mm.Items.Add(mi);
    mi.Caption := 'A';
    (FindComponent('AA1') as TMenuItem).Add(mi);
    [/code]
    Which make the complete code into:
    [code]
    mi:=TMenuItem.Create(Self); && self is the TForm
    mi.Caption:=Menu['Caption'];
    mi.Name:=Menu['ITEM'];
    if Menu.FieldByName('Parent').IsNull then
    mm.Items.Add(mi)
    else
    (FindComponent( Menu['PARENT'] ) as TMenuItem).Add(mi);
    [/code]
  • Oh MY GOOOD, i'm really stupid :)))

    Thanks a lot zibadian, u saved me from crazyness just like many times before :)) Thanks a lot, man!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories