组件设计期动态生成控件

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分..
...全文
113 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
i54 2008-09-23
  • 打赏
  • 举报
回复
谢谢SUTON,能试出来都是有SUTON帮助,所以分数全归他
i54 2008-09-23
  • 打赏
  • 举报
回复
我刚刚试出来了.
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
i54 2008-09-23
  • 打赏
  • 举报
回复
TO:Suton
能不能把近来的控件代码共享出来学习学习。。。
我的邮箱:
i54 2008-09-23
  • 打赏
  • 举报
回复
TO:hys_427
DFM是设计期的文件,就是在FORM上右键-.>view as text就可以看到里面内容.
i54 2008-09-23
  • 打赏
  • 举报
回复
谢谢Suton,我试试,OK马上给分.....
suton 2008-09-22
  • 打赏
  • 举报
回复
我是估计楼主是想实现类似TPageControl的Add Page的功能.
在设计时,从控件的右键菜单中,选择一项自定义的功能(如TPageControl的Add Page),就能给控件添加子控件(TTabSheet),
同时dfm文件里面也保存添加的TTabSheet的相关信息.

刚好我最近做个控件也用到了这个自定义属性编辑器,所以有点心得.
喝口水 2008-09-22
  • 打赏
  • 举报
回复
如何能动态生成TEDIT并可以在保存在DFM文件上

动态生成TEDIT比较好办,但是保存在DFM文件中不好做,不知楼主要干什么,DFM是设计期文件
suton 2008-09-22
  • 打赏
  • 举报
回复
哦,看错了,是放在两个单元里的....

把CreateEdit设为public,不然外面访问不了.
其实关键就是:Designer.Modified;

Designer是个接口,调用Modified后,它会自动更新dfm文件.
suton 2008-09-22
  • 打赏
  • 举报
回复
另外,你的Editor的类,最好和你控件的类,放在不同的单元,不然,你会碰到Proxies.dcu文件找不到的问题.
suton 2008-09-22
  • 打赏
  • 举报
回复
procedure TEditcomponentEditor.ExecuteVerb(Index: Integer);
begin
//inherited;
case index of
0:
begin
//
end;
1:
begin
messagedlg('11',mtinformation,[mbOK],0);

//在这里调用:
(Component as TMYpanel).CreateEdit;
Designer.Modified;

end;
end;
end;
i54 2008-09-22
  • 打赏
  • 举报
回复
up...
i54 2008-09-22
  • 打赏
  • 举报
回复
在线等......
OK马上给分..

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧