组件设计期动态生成控件.组件在元件编辑器上动态生成控件如Tedit.生成的Tedit是一个全局的变量,直接是保存在DFM文件上.
我代码如下:(可能生成Tedit但是不能保存在DFM文件上)//组控文件
unit Testpanel;interfaceuses
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;
beginend;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);
beginend;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分..
我代码如下:(可能生成Tedit但是不能保存在DFM文件上)//组控文件
unit Testpanel;interfaceuses
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;
beginend;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);
beginend;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分..
begin
//inherited;
case index of
0:
begin
//
end;
1:
begin
messagedlg('11',mtinformation,[mbOK],0);
//在这里调用:
(Component as TMYpanel).CreateEdit;
Designer.Modified;
end;
end;
end;
其实关键就是:Designer.Modified;Designer是个接口,调用Modified后,它会自动更新dfm文件.
在设计时,从控件的右键菜单中,选择一项自定义的功能(如TPageControl的Add Page),就能给控件添加子控件(TTabSheet),
同时dfm文件里面也保存添加的TTabSheet的相关信息.刚好我最近做个控件也用到了这个自定义属性编辑器,所以有点心得.
DFM是设计期的文件,就是在FORM上右键-.>view as text就可以看到里面内容.
能不能把近来的控件代码共享出来学习学习
][/email]
procedure TEditcomponentEditor.Edit;
var
ed1:Tedit;
begin
ed1:=Tedit.Create(designer.Root);
ed1.Left:=100;
ed1.Top:=100;
ed1.Width:=100;
ed1.Height:=20;
ed1.Parent:=Tpanel(component);
designer.Modified;
end; //create里要用到desinger.Root,但这个变量是在设计期中生成,并不是Tpanel类里变定义的变量
FWEdit:TmyEdit;
TmyEdit=array [1..10] of Tedit;
生成的EDIT会在DFM中保存,运行也正常。能不能用到Tpanel里面的变里FWEDIT:TMYEDIT