TWebMainMenu, Remove, Insert

Dear Sirs,

TMS Web Core 1.6.2.0
RAD Studio Sydney (Delphi 10.4 Version 27.0.38860.1461)

Please see attached project WebCoreBugMainMenu.zip (307.1 KB)
Click button "Run" to expose the problem.

MenuItem.Insert

When we try to insert a menu item at a specific index we get this error:
Uncaught TypeError: Cannot read property 'UpdateElement' of null | TypeError: Cannot read property 'UpdateElement' of null at Object.Insert$1

The problem is that "Parent" is not assigned when item is inserted.

Example code:
LIndex := 2;
LMenuItemNew := TMenuItem.Create(nil);
LMenuItemNew.Name := 'mi1';
LMenuItemNew.Tag := 1;
LMenuItemNew.Caption := '1';
LMenuItemNew.Enabled := True;
LParentMenuItem.Insert(LIndex, LMenuItemNew);

Other variations of this example also fail with same problem:
LIndex := 2;
LMenuItemNew := TMenuItem.Create(LParentMenuItem);
LMenuItemNew.Name := 'mi1';
LMenuItemNew.Tag := 1;
LMenuItemNew.Caption := '1';
LMenuItemNew.Enabled := True;
LParentMenuItem.Insert(LIndex, LMenuItemNew);

Only this example works but we can not change menu item index:
LMenuItemNew := TMenuItem.Create(nil);
LMenuItemNew.Name := 'mi1';
LMenuItemNew.Tag := 1;
LMenuItemNew.Caption := '1';
LMenuItemNew.Enabled := True;
LMenuItemNew.SetParentComponent(LParentMenuItem);

MenuItem remove

Current workaround for item removal is to find menu item (LMenuItem) and run:
LParentMenuItem.RemoveComponent(LMenuItem); // removes item
LMenuItem.Free; //removes visual (html/dom) item

Not yet supported calls would be prefered:
TMenuItem.Delete(Index: Integer); //[Error] uMain.pas(58): identifier not found "Delete"
TMenuItem.Remove(Item: TMenuItem); // [Error] uMain.pas(57): Can't access private member Remove

Kind regards

Hi,

Thank you for notifying.
We are currently investigating these issues and will report back as soon as possible.

MenuItem insert
This behaviour should be improved with the next release of TMS WEB Core.

MenuItem remove

Can you please try using the following?
LParentMenuItem.Items.Delete(Index);

We adjusted the project and it is working without errors in TMS Web Core 1.6.2.0 also.
Adjusted project for anyone interested WebCoreBugMainMenu.zip (312.7 KB)

procedure TfrmMain.ButtonClick(Sender: TObject);
var
LMenuItem: TMenuItem;
LMenuItemNew: TMenuItem;
LParentMenuItem: TMenuItem;
LIndex: Integer;
I: Integer;
begin
WebMainMenu.BeginUpdate;
try
LParentMenuItem := miGroup;
LIndex := 3;

for I := LParentMenuItem.Items.Count-1 downto 0 do
begin
  LMenuItem := LParentMenuItem.Items[I];
  if LMenuItem.Tag>0 then
    LParentMenuItem.Items.Delete(I);
end;

LMenuItemNew := TMenuItem.Create(nil);
LMenuItemNew.Name := 'mi1';
LMenuItemNew.Tag := 1;
LMenuItemNew.Caption := '1';
LMenuItemNew.Enabled := True;
LParentMenuItem.Items.Insert(LIndex, LMenuItemNew);

LMenuItemNew := TMenuItem.Create(nil);
LMenuItemNew.Name := 'mi2';
LMenuItemNew.Tag := 2;
LMenuItemNew.Caption := '2';
LMenuItemNew.Enabled := True;
LParentMenuItem.Items.Insert(LIndex+1, LMenuItemNew);


LMenuItemNew := TMenuItem.Create(nil);
LMenuItemNew.Name := 'miD';
LMenuItemNew.Tag := 3;
LMenuItemNew.Caption := '-';
LMenuItemNew.Enabled := True;
LParentMenuItem.Items.Insert(LIndex+2, LMenuItemNew);

finally
WebMainMenu.EndUpdate;
end;
end;

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.