Jump to content

Dynamically created tuniMenuItems cannot be removed


Recommended Posts

Posted

Hi, it's me again, the guy who likes to create controls at runtime, giving you headaches... ;-)

 

This time: TuniPopupMenu

var mn: TUniMenuItem;
    popMenu.Items.Clear;  //dont work
    while popMenu.Items.Count > 0 do begin  //also don't work
      mn:= popMenu.Items[pmMastertables.Items.Count - 1];
      popMenu.Items.Remove(mn);
      mn.free;
    end;

    for ix:= 0 to List.Count - 1 do begin
      mn:= TUniMenuItem.Create(self);
      with mn do begin
        Caption:= List[ix];
        Hint:= 'This does something';
        OnClick:= DoSomethingClick;
      end;
      popMenu.Items.Add(mn);
    end;

Works fine to add menuitems, but the removal code at the beginning does not work in web mode. So whenever I call this routine again to populate an EMPTY menu with NEW items, the old items remain.

 

I've tried everything... Do I need to call some refresh somewhere?!

 

Posted

Hello Farshad, can you confirm if this is a bug?

Even setting the menu items to invisible is ignored.

    popMenu.Items.Clear;  //dont work
    while popMenu.Items.Count > 0 do begin
      mn:= popMenu.Items[pmMastertables.Items.Count - 1];
      mn.visible:= false;  //don't work
      popMenu.Items.Remove(mn);  //don't work
      mn.free;
    end;
Posted
Hi Michael Schindler.

 

Try this:



...
type
  TxPopupMenu = class(TuniPopupMenu)
  end;
...



procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  //Items.clear
  uniSession.AddJS(TxPopupMenu(uniPopupMenu1).GetMenuControl.JSName + '.items.clear();');
end;



Posted

Did that. Now the number of menuitems is correct, but they are somewhat painted over the old ones and cannot be clicked either. So I'm actually worse off.

 

My code:

    uniSession.AddJS(TxPopupMenu(pmMasterTables).GetMenuControl.JSName + '.items.clear();');
    pmMastertables.Items.Clear;

Result:

 

image04.jpg

 

Farshad, can you please confirm this to be a bug?

How do I dynamically populate a popup menu?

 

I have no use for a popup menu which I cannot populate dynamically from code. I need to be able to add/delete menuitems in runtime.

Posted
Hi Michael Schindler. 

 

You are right. 

 

Solution: add the update() after items clear: 



procedure TMainForm.UniButton2Click(Sender: TObject);
begin
//Items.clear
UniSession.AddJS(TxPopupMenu(pmMasterTables).GetMenuControl.JSName + '.items.clear();');
  UniSession.AddJS(TxPopupMenu(pmMasterTables).GetMenuControl.JSName + '.update();');
//pmMastertables.Items.Clear;
end;


Best regards.

Posted

Perfect. it works. How do you find these kind of solutions?!

 

Also, can you explain to me why I have to typecast like this?

TxPopupMenu = class(TuniPopupMenu)
uniSession.AddJS(TxPopupMenu(pmMasterTables).GetMenuControl.JSName + '.items.clear();');

Should I post this into bug reports, because obviously "pmMastertables.Items.Clear;" is not working?

Posted
Hi Michael Schindler. 

 

In brief I can say: 

 

1. How do you find these kind of solutions?!    -   Well, on the Internet, in forums, using debuggers ... 

 

2. TxPopupMenu(pmMasterTables).GetMenuControl.JSName    -   This trick provides access to protected methods of a class ... 


 

3. Should I post this into bug reports, because obviously "pmMastertables.Items.Clear;" is not working?   -   I do not know this is a bug or not implemented yet ... 

 

Sincerely ...

  • Administrators
Posted

 

Hi Michael Schindler. 
 
In brief I can say: 
 
1. How do you find these kind of solutions?!    -   Well, on the Internet, in forums, using debuggers ... 
 
2. TxPopupMenu(pmMasterTables).GetMenuControl.JSName    -   This trick provides access to protected methods of a class ... 
 
3. Should I post this into bug reports, because obviously "pmMastertables.Items.Clear;" is not working?   -   I do not know this is a bug or not implemented yet ... 
 
Sincerely ...

 

 

Actually, not implemented.

  • 5 years later...
Posted

This worked for me for a TUniMainMenu

var
  I : Integer;
begin
  for I := mnuTabList.Count - 1 downto 0 do  // Index backwards because Count changes as you remove items.
    if mnuTabList.Items.Name = 'mnuStoreSettings' then  // Items created dynamically.
      mnuTabList.Items.Free;

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...