动态添加一个菜单,想把这个菜单的onclick事件与一个函数对应起来,可是报错!!
unit Unit3;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus;var
  pop :  TPopupMenu;
  i1: TMenuItem;
  i2: TMenuItem;procedure popMenu();
implementation
  procedure i1Click(Sender: TObject);
  begin
    showmessage('dddd');
  end; procedure popMenu();
 begin
  pop :=  TPopupMenu.Create(nil);
  i1 := TMenuItem.Create(pop);
  i2 := TMenuItem.Create(pop);
  i1.Caption :='y1';
  i2.Caption :='y2';  i1.OnClick := i1Click;//此处报错!!!!
  pop.Items.Insert(0,i1);
  pop.Items.Insert(1,i2);  pop.Popup(100,100); end;
end.错误::
[Error] Unit3.pas(31): Incompatible types: 'method pointer and regular procedure'
[Fatal Error] Project1.dpr(8): Could not compile used unit 'Unit3.pas'
到底怎么连接这个事件!!!谢谢!!!!

解决方案 »

  1.   

    动态添加后没在uses中声明当然回出现错误了
      

  2.   

    procedure i1Click(Sender: TObject);
    必须申明?
      

  3.   

    像楼上所说,我在procedure popMenu();这句下面加上procedure  i1Click(Sender: TObject);
    还是有错误!!!
    错误::
    [Error] Unit3.pas(31): Incompatible types: 'method pointer and regular procedure'
    [Fatal Error] Project1.dpr(8): Could not compile used unit 'Unit3.pas'
      

  4.   

    i1Click只是个普通的方法,要给Onclick赋值的时候接受的要是TNofifyEvent方法才行。你的i1Click方法要这样声明:TForm1=class(TForm)
    private
      procedure i1Click(Sender:TObject);  //把方法放在某个类里
    end;implementationprocedure TForm1.i1Click(Sender: TObject);
    begin
      showmessage('dddd');
    end;
      

  5.   

    我按照楼上说的修改了
    unit Unit3;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus;type  TForm1=class(TForm)
    private
      procedure i1Click(sender:TObject);//把方法放在某个类里
    end;var
      pop :  TPopupMenu;
      i1: TMenuItem;
      i2: TMenuItem;
    procedure  popMenu();implementation  procedure TForm1.i1Click(sender: TObject);
      begin
        showmessage('dddd');
      end; procedure popMenu(); begin
      pop :=  TPopupMenu.Create(nil);
      i1 := TMenuItem.Create(pop);
      i2 := TMenuItem.Create(pop);
      i1.Caption :='y1';
      i2.Caption :='y2';  i1.OnClick := i1Click;//还是出错
      pop.Items.Insert(0,i1);
      pop.Items.Insert(1,i2);  pop.Popup(100,100); end;
    end.
    错误:
    [Error] Unit3.pas(38): Undeclared identifier: 'i1Click'
    [Hint] Unit3.pas(13): Private symbol 'i1Click' declared but never used
    [Fatal Error] Project1.dpr(8): Could not compile used unit 'Unit3.pas'
    到底怎么回事呀!!!!
      

  6.   

    一句话讲不完啊.你的程序没有窗体的吗?如果有窗体的话, 应该就会有类似
    TForm1=class(TForm)
    end;var
      Form1:TForm1;之类的语句。这时就像我之前说的那样改,然后将
     i1.OnClick := i1Click;//还是出错
    改成
     i1.OnClick := Form1.i1Click; //Form1是i1Click所在的类的对象,必须已经创建。
      

  7.   

    楼议楼主好好看一下Object Pascal的书,看一下函数指针的部分。
      

  8.   

    我想做一个没有窗体的unit,在这个unit里可以动态生成菜单,然后那个窗体要用到这个菜单就去调用这个unit中的popMenu函数就可以了!!!就算没有窗体也可以生成菜单,现在就是点击菜单后触发不了事件,所以有没有窗体应该不是问题吧!!!就算有窗体也不行呀!!!
      

  9.   

    窗体类也不是必须的,唯一必须的,是你的i1Click必须是方法,而不是普通的过程或者函数