组件设计期动态生成控件
i54 2008-09-22 05:44:21 组件设计期动态生成控件.组件在元件编辑器上动态生成控件如Tedit.生成的Tedit是一个全局的变量,直接是保存在DFM文件上.
我代码如下:(可能生成Tedit但是不能保存在DFM文件上)
//组控文件
unit Testpanel;
interface
uses
SysUtils, Classes, Controls, ExtCtrls,DB,ADODB,StdCtrls,dialogs,windows,Forms;
type
TmyEdit=array [1..10] of Tedit;
TMYpanel = class(TPanel)
private
Flog:string;
FDataSource:TDataSource;
FWEdit:TmyEdit;
FAct:boolean;
function GetLog: string;
procedure SetLog(const Value: string);
function GetAct: Boolean;
procedure SetAct(const Value: Boolean);
procedure CreatEdit;
procedure FreeEdit;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property log:string read GetLog write SetLog;
property Act:Boolean read GetAct write SetAct;
property DataSource:TDataSource read FDataSource Write FDataSource;
end;
implementation
{ TMYpanel }
procedure TMYpanel.CreatEdit;
var
i:integer;
begin
try
for i:=0 to 10 do
begin
if assigned(self.fwedit[i]) then
begin
self.FWEdit[i].Free;
self.FWEdit[i]:=nil;
end;
end;
for i:=0 to 10 do
begin
self.FWEdit[i]:=Tedit.Create(self);
self.FWEdit[i].Top:=(i div 3 )*30;
self.FWEdit[i].Left:=(i mod 3)*150;
self.FWEdit[i].Width:=120;
self.FWEdit[i].Height:=20;
self.FWEdit[i].Parent:=self;
end;
except
exit;
end;
end;
procedure TMYpanel.FreeEdit;
var
i:integer;
begin
try
for i:=0 to 10 do
begin
if assigned(self.fwedit[i]) then
begin
self.FWEdit[i].Free;
self.FWEdit[i]:=nil;
end;
end;
except
exit;
end;
end;
function TMYpanel.GetAct: Boolean;
begin
result:=Fact;
end;
function TMYpanel.GetLog: string;
begin
end;
procedure TMYpanel.SetAct(const Value: Boolean);
begin
try
FAct:=value;
if FAct=true then
begin
creatEdit;
end;
if FAct=false then
begin
FreeEdit;
showmessage('false');
end;
except
end;
end;
procedure TMYpanel.SetLog(const Value: string);
begin
end;
end.
注:组件文件可以动态生成TEDIT控件,但是不能保存在DFM文件里.还有一个问题就是TEDIT私有变量,修改不了TEDIT
//注册文件
unit reg;//注册的文件
interface
uses DesignIntf,DesignEditors, StdCtrls,Uform,DsnDBCst, DesignWindows, TypInfo,
Windows, SysUtils, Messages, Classes, Graphics, Controls, Forms,
ExtCtrls, DB, DBCtrls;
type
TEditcomponentEditor=class(TComponentEditor)//元件编辑器
private
public
procedure Edit;override;
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
end;
procedure Register;
implementation
uses testPanel,ADODB,dialogs;
procedure Register;//注册
begin
RegisterComponentEditor(TMYpanel,TEditcomponentEditor);
RegisterComponents('AMy', [TMYpanel]);
end;
{ TEditcomponentEditor }
procedure TEditcomponentEditor.Edit;
var
i:integer;
begin
//inherited;
try
if (component is TMYpanel) then
begin
for i:=0 to 10 do
begin
TMYpanel(component).FWEdit[i]:=Tedit.Create(TMYpanel(component));
TMYpanel(component).FWEdit[i].Top:=(i div 3)*30;
TMYpanel(component).FWEdit[i].Left:=(i mod 3)*150;
TMYpanel(component).FWEdit[i].Width:=120;
TMYpanel(component).FWEdit[i].Height:=20;
TMYpanel(component).FWEdit[i].Parent:=TMYpanel(component);
end;
end;
except
exit;
end;
end;
procedure TEditcomponentEditor.ExecuteVerb(Index: Integer);
begin
//inherited;
case index of
0:
begin
//
end;
1:
begin
messagedlg('11',mtinformation,[mbOK],0);
end;
end;
end;
function TEditcomponentEditor.GetVerb(Index: Integer): string;
begin
case index of
0:result:='00';
1:result:='11';
end;
end;
function TEditcomponentEditor.GetVerbCount: Integer;
begin
result:=2;
end;
end.
请各位大侠指点,如何能动态生成TEDIT并可以在保存在DFM文件上....送上100分..