How to dynamically update items of popupmenu ?
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
...
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
procedure PopupClick(Sender: TObject);
...
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
begin
with PopupMenu1, Items do
begin
// Removing all items
while Count > 0 do
Delete(0);
// Adding first Item
Add(NewItem('Item 1', 0, False, True, PopupClick, 0, 'Item1'));
// Adding second Item
Add(NewItem('Item 2', 0, False, True, PopupClick, 0, 'Item2'));
// Adding a separator
Add(NewLine);
// Adding a third item with two submenus
Add(NewSubMenu('Item 3', 0, 'Item3', [NewItem('SubItem 1', 0,
False, True, PopupClick, 0, 'SubItem1'),
NewItem('SubItem 2', 0, False, True, PopupClick, 0,
'SubItem2')]));
with ClientToScreen(Point(X, Y)) do
Popup(X, Y);
end;
end;
end;
procedure TForm1.PopupClick(Sender: TObject);
begin
// Using here a stupid way to know which menu is triggering the onclick
// event. Use the tag property instead.
case (Sender as TMenuItem).MenuIndex of
0: ShowMessage('Item 1');
1: ShowMessage('Item 2');
end;
end;