2、为Button1赋以下事件响应:
procedure Tform1.Button1Click(Sender:Tobject);
Var
Menuitem:array[1..4]of Tmenuitem;
I:integer;
begin
While(popupmenu1.items.count > 0)then
popupmenu1.items.delete(0);
for I:=1 to 4 do
begin
Menuitem=TmenuItem.Create(Self);
//---动态创建TMenuItem
MenuItem.Caption=inttostr(I);
//---设置MenuItem的Caption属性
MenuItem.OnClick:=ShowIt;
//----定义menuItem的OnClick事件的处理过程
PopupMenu1.Items.Add(MenuItem);
//----增加到PopMenu中
end;
end;
其中,ShowIt是程序中自制的Procedure。
Procedure Tform1.ShowIt(Sender:Tobject);
begin
With Sender as TmenuItem do
begin
ShowMessage(Caption);
end;
end;
下面是我要赋给每个菜单OnClick事件的过程
procedure TMainFrm.MyMenuOnClick(Sender: TObject);
var
AComponent: TComponent;
str: string;
i : integer;
begin
with Sender as TMenuItem do
begin
str := Name;
AComponent := application.FindComponent(str);
if Assigned(AComponent) then
TForm(AComponent).Show; //显示与当前菜单名相同的Form
end;
end;