问题描述
我正在使用包含事件处理程序的某些组件的表单。现在,我想将那些事件处理程序(那些方法)移到一个单独的单元,仍然可以在设计时通过对象检查器将它们分配给组件事件。
I'm having a form with certain components whose are having event handlers. Now I would like to move those event handlers (those methods) to a separate unit still being able to assign them to component events through the Object Inspector at design time.
是可以为事件方法只创建一个单独的单元,以便对象检查器允许我在设计时分配它们?
Is it possible to make a separate unit only for event methods, so that the Object Inspector allows me to assign them at design time ?
让我说是否要使用公开程序:
Let's say if I would make that unit with a public procedure:
unit Unit2;
interface
procedure ButtonClick(Sender: TObject);
implementation
procedure ButtonClick(Sender: TObject);
begin
// do something here
end;
end.
或者使用具有此类发布方法的类:
Or with a class with published method like this:
unit Unit2;
interface
type
TMyClass = class
published
procedure ButtonClick(Sender: TObject);
end;
var
MyClass: TMyClass;
implementation
{ TMyClass }
procedure TMyClass.ButtonClick(Sender: TObject);
begin
// do something here
end;
end.
如何为事件方法创建单独的单元,IDE允许我在设计时将其分配给组件事件时间 ?例如,例如:
How to make a separate unit for event methods, which IDE allows me to assign to component events at design time ? Like for instance:
推荐答案
大多数事件是方法指针。这意味着它们指向类中的过程或函数。因此,您不能仅将过程Unit2.ButtonClick附加到按钮的on click事件上,而是可以编写一个实现事件处理程序的类,如下所示:
Most events are method pointers. That means they point to a procedure or function in a class. So you cannot just attach the procedure Unit2.ButtonClick to an on click event of a button, but you can write a class that implements the event handler, something like this:
type
TMainFormButtonEventHandler = class
procedure ButtonClick(Sender: TObject);
end;
procedure TMainFormButtonEventHandler.ButtonClick(Sender: TObject);
begin
ShowMessage('Clicked');
end;
现在您可以创建这样的对象并将其链接到事件:
Now you can create such an object and link it to the event:
handler := TMainFormButtonEventHandler.Create;
Form1.Button1.OnClick := handler.ButtonClick;
不过,我认为这不是最好的应用程序结构。我不会从表单单元外部加入表单的GUI元素。但是,如果您愿意的话,就是这样做的。
I don't think this is the best application structure, though. I would not hook into GUI elements of a form from outside the form's unit. But if you would like to do so, this is how it's done.
如果您要分离GUI和逻辑,请查看操作。 TAction(包装在ActionList中)是GUI组件(如按钮)与其执行的动作代码之间的第一层抽象。
If you're looking for separation of GUI and logic, have a look at actions. A TAction (wrapped in an ActionList), is the first layer of abstration between a GUI component like a button and the action code it performs.
方便的事情是,您也可以在设计时创建这些动作,并将它们附加到按钮或其他控件上。您无需为ButtonClick编写代码,而为ActionExecute(操作的OnExecute事件)编写代码。该按钮知道单击该按钮时,应该执行其相关操作。
The convenient thing is that you can create those actions at design time as well, and attach them to a button or other control. Instead of writing code for ButtonClick, you write code for ActionExecute (the OnExecute event of the action). The button knows that when it is clicked, it should execute its related action.
这篇关于如何为事件方法制作一个单独的单元,哪个IDE允许我在设计时分配给组件事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!