Delphi动态创建菜单

这篇博客介绍了一个在Delphi中动态创建菜单的实现过程。通过代码示例展示了如何清除旧菜单项,根据数据库查询结果动态插入新菜单项,并为每个菜单项设置点击事件。同时,文中还指出了在处理菜单项的Caption属性时需要注意的细节问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

procedure TForm1.PopupMenu2ListSender();
var
    addSubItem:TMenuItem;
    j:Integer;
begin
  QueryPopMenu.Close ;
  QueryPopMenu.FetchParams ;
  QueryPopMenu.Params.ParamByName('AREA').AsString :=Area;
  QueryPopMenu.Open ; 
  While(PM2Count>0) do
  Begin
    PopupMenu2.Items[PM2Count-1].Free ;  //if from 0 to PM2Count-1 cann't get the correct                                  result,unless take the desc .
    Dec(PM2Count);
  End;
  PM2Count:=0;
  While(not QueryPopMenu.Eof)do
  Begin
    addSubItem:= TMenuItem.Create(Self);
    addSubItem.Name := QueryPopMenu.fieldByName('eqpgroup').AsString;

    addSubItem.Caption := QueryPopMenu.fieldByName('eqpgroup').AsString; 
    PopupMenu2.Items.Insert(PM2Count,addSubItem);
    addSubItem.OnClick :=PopupMenu2Click;
    INC(PM2Count);
    QueryPopMenu.Next ;
  End;
end;

procedure TForm1.PopupMenu2Click(Sender: TObject);
Begin
      Fab := TMenuItem(Sender).Name;

//if take the TMenuItem(Sender).Caption the first letter has a underline , so we cann't get the correct value.
      SelectKeyGroup;
End;

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值