delphi - 我如何在 DELPHI 中任何单元的初始化块内调用任何过程或函数
问题描述
我试图在我的单元的初始化块中调用一个过程,因为我希望该过程在应用程序启动时执行,然后再执行其他任何操作。编译器显示此错误:
[dcc32 Error] Unit2.pas(152): E2076 This form of method call only allowed for class methods or constructor
这就是我的程序的样子
procedure TForm2.initilize() ;
begin
ListBox2.Items.Add('Mohit');
ListBox2.Items.Add('Raghav');
ListBox2.Items.Add('Maninder');
ListBox2.Items.Add('Tanya');
end;
这是在初始化中进行调用的地方
initialization
begin
TForm2.initilize();
end;
解决方案
我更喜欢经典的构造函数。优点是您甚至可以对框架使用相同的方法,因为框架没有 OnCreate。
interface
...
type
TForm2 = class(TForm)
private
procedure initialize;
...
public
constructor Create(AOwner: TComponent); override;
...
end;
implementation
constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
initialize;
end;
推荐阅读
- java - 如何使用 Struts 2 jQuery 解决错误设置日期
- sql - 执行递归查询时检测到循环
- c - 当 x=0x80000000, y = 1(32 位补码)时,为什么 `x - y <= x` 为真?
- image - 如何在初始屏幕中使用多个图像进行反应原生
- android - Android Auto Desktop Head Unit (DHU) Hello World 应用程序已启动,但 DHU 中没有可用的启动器图标
- html - react-masonry-component 不在父 div 中居中
- pytorch - 如何在使用 ONNX 推理会话时通过传递“标签”来获得语言建模损失?
- ios - 具有相同基类单元的委托调用的多个控制器中的 DelegateProtocol 问题
- r - 枢轴更长的包机标题
- powerbi - 计算产品销售额大于 0 的月份数