如何为Delphi TFrame对象实现OnCreate事件

TFrame is a container for components; it can be nested within forms or other frames.

TFrame是组件的容器; 它可以嵌套在表单或其他框架中。

A frame, like a form, is a container for other components. Frames can be nested within forms or other frames, and they can be saved on the Component palette for easy reuse.

框架像表单一样,是其他组件的容器。 框架可以嵌套在表单或其他框架中,并且可以保存在“组件”面板中以方便重用。

缺少OnCreate ( Missing OnCreate )

Once you start using frames, you'll note there's no OnCreate event you can use to initialize your frames.

一旦开始使用框架,您会注意到没有可用于初始化框架的OnCreate事件。

In short, the reason that a frame does not have a OnCreate event is there is no good time to fire the event.

简而言之,框架没有OnCreate事件的原因是没有时间触发该事件。

However, by overriding the Create method you can mimic the OnCreate event. After all, the OnCreate for Forms gets fired at the end of the Create constructor — so overriding Create for Frames is as having the OnCreate event.

但是,通过重写Create方法,您可以模仿OnCreate事件。 毕竟,在Create构造函数的末尾会触发OnCreate for Forms —因此,覆盖Create for Frames就像具有OnCreate事件一样。

Here's the source code of a simple frame exposing a public property and overriding the Create constructor:

这是暴露公共属性并覆盖Create构造函数的简单框架的源代码:

 unit WebNavigatorUnit;
interface
 uses
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, StdCtrls;
 type
   TWebNavigatorFrame = class(TFrame)

    urlEdit: TEdit;
urlEdit:TEdit;
private
     fURL: string;
procedure SetURL(const Value: string) ;
public
     constructor Create(AOwner: TComponent) ; override;
published
     property URL : string read fURL write SetURL;
end;
implementation{$R *.dfm}
 constructor TWebNavigatorFrame.Create(AOwner: TComponent) ;
begin
   inherited Create(AOwner) ;
 //"OnCreate" code
   URL := 'http://delphi.about.com';
 end;
procedure TWebNavigatorFrame.SetURL(const Value: string) ;
begin
   fURL := Value;
   urlEdit.Text := Value;
 end;
end.

The "WebNavigatorFrame" acts as a website launcher hosting an edit and a button control. Note: if you are new to frames, make sure you read the following two articles: visual component development using frames, replacing tabsheets with frames.

“ WebNavigatorFrame”充当托管编辑和按钮控件的网站启动器 。 注意:如果您不熟悉框架 ,请确保阅读以下两篇文章: 使用框架进行可视化组件开发,用框架 替换选项卡。

翻译自: https://www.thoughtco.com/implement-oncreate-event-delphi-tframe-object-1057878

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DELPHI动态创建删除FRAME unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,FM; type TForm2 = class(TForm) Panel1: TPanel; Button2: TButton; ScrollBox1: TScrollBox; procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type TFM = Array Of TFrame1; var Form2: TForm2; aFM: TFM; procedure DeleteArrItem(var arr: TFM ; Index: Integer);stdcall; implementation {$R *.dfm} procedure DeleteArrItem(var arr: TFM ; Index: Integer); var Count: Cardinal; i:integer; begin Count := Length(arr); if (Count = 0) or (Index = Count) then Exit; Move(arr[Index+1], arr[Index], (Count-Index)* SizeOf(arr[0])); SetLength(arr, Count - 1); for I := 0 to Length(arr) - 1 do begin arr[i].Label1.Caption:=inttostr(i); end; end; procedure TForm2.Button2Click(Sender: TObject); var fram:TFrame1; begin SetLength(aFM,length(aFM)+1); aFM[length(aFM)-1] :=TFrame1.Create(nil) ; fram:=aFM[length(aFM)-1]; fram.Label1.Caption:=inttostr(length(aFM)-1); fram.Parent:=ScrollBox1; end; end. unit fm; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TFrame1 = class(TFrame) GroupBox1: TGroupBox; Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; implementation uses unit2; {$R *.dfm} procedure TFrame1.Button1Click(Sender: TObject); begin DeleteArrItem(aFM,strtoint(label1.Caption)); ( Sender as Tbutton ).Parent.Parent.Destroy; end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值