Delphi:当我点击一个控件时如何调用方法? [英] Delphi: How to call a method when i click a control?
本文介绍了Delphi:当我点击一个控件时如何调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法:
procedure Frob(Sender: TObject);
我想在点击菜单项时调用。
that i want to call when i click a menu item.
该方法通过界面来找我:
The method comes to me though an interface:
animal: IAnimal;
IAnimal = interface
procedure Frob(Sender: TObject);
end;
问题围绕分配给 OnClick $ c $的内容c>菜单项的事件处理程序(即控件):
The question revolves around what to assign to the OnClick
event handler of a menu item (i.e. control):
var
animal: IAnimal;
...
begin
...
menuItem := TMenuItem.Create(FileMenu)
menuItem.Caption := 'Click me!';
menuItem.OnClick := <-------- what to do
...
end;
明显的选择,我的第一次尝试,错误的答案是:
menuItem.OnClick := animal.Frob;
那么当用户点击某个控件时, 如何调用方法?
So how can i call a method when user clicks a control?
- Why doesn't it work?
推荐答案
在私人领域中拥有你所拥有的任何物体,然后设置一个调用它的方法。像这样:
Have whatever object you're in hold the animal in a private field, then set up a method that calls it. Like so:
procedure TMyClass.AnimalFrob(Sender: TObject);
begin
FAnimal.Frob(sender);
end;
然后解决方案变得简单:
Then the solution becomes easy:
menuItem.OnClick:= self.AnimalFrob;
这篇关于Delphi:当我点击一个控件时如何调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文