将事件侦听器分配给在Delphi中动态创建的TPanel

时间:2019-04-17 19:50:58

标签: delphi

我这样创建了一个TPanel组件:

procedure TVistaVehiculo.CrearMenu(Name: string);
var
  Panel : TPanel;
begin
  Panel := TPanel.Create(VistaVehiculo);
  Panel.Parent := VistaVehiculo.Sidebar;
  Panel.Width := VistaVehiculo.Sidebar.Width;
  Panel.Height := 40;
  Panel.Caption := Name;
  Panel.BevelInner := TBevelCut.bvNone;
  Panel.BevelOuter := TBevelCut.bvNone;
  Panel.BevelKind := TBevelKind.bkNone;
end;

现在,我想在该面板上附加一个事件侦听器,并将对象作为参数传递。

因此,我创建了一个像这样的过程:

procedure TVistaVehiculo.ClickOnMenu(Sender: TPanel);
begin
    Sender.Caption := 'Clicked'; //for example
end;

我该怎么做?

抱歉,问题很愚蠢-我对Delphi还是很陌生

1 个答案:

答案 0 :(得分:3)

procedure TVistaVehiculo.CrearMenu(Name: string);
var
  Panel : TPanel;
begin
  Panel := TPanel.Create(VistaVehiculo);
  Panel.Parent := VistaVehiculo.Sidebar;
  Panel.Width := VistaVehiculo.Sidebar.Width;
  Panel.Height := 40;
  Panel.Caption := Name;
  Panel.BevelInner := TBevelCut.bvNone;
  Panel.BevelOuter := TBevelCut.bvNone;
  Panel.BevelKind := TBevelKind.bkNone;
  Panel.OnClick := ClickOnMenu; // <-- add this!
end;

procedure TVistaVehiculo.ClickOnMenu(Sender: TObject); // <-- must be TObject!
begin
  TPanel(Sender).Caption := 'Clicked';
end;