看看问题300 + 解决问题另外300 = 600(300分)

  • 主题发起人 Another_eYes
  • 开始时间
A

Another_eYes

Unregistered / Unconfirmed
GUEST, unregistred user!
一个没用处的控件代码, 只是为说明问题:
type
TNoUsage = class(TPanel)
private
{ Private declarations }
fedit1: TEdit;
fedit2: TEdit;
fedit3: TEdit;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;

published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TNoUsage]);
end;

constructor TEdPanel.Create(AOwner: TComponent);
begin
inherited Create(aowner);
controlstyle := controlstyle - [csAcceptsControls];
fedit1 := tedit.create(aowner);
fedit1.parent := self;
fedit1.left := 5;
fedit1.top := 5;
fedit2 := tedit.create(aowner);
fedit2.parent := self;
fedit2.left := 5;
fedit2.top := 35;
fedit3 := tedit.create(aowner);
fedit3.parent := self;
fedit3.left := 5;
fedit3.top := 65;
end;

end.
上述控件设计时放到form中, 将在panel中内建了3个edit, 各位可以发觉
鼠标点击任何一个edit时, object inspector中将允许修改该edit的属性
现在大家应该发觉了一个问题:
这个edit的name是空的.
请问: 如何让IDE自动添入name(也自动添入text)属性内容, 比如
edit1, edit2,edit3?
别告诉我不可能: TPageControl中的TTabSheet做到了.
 
看是Another_Eyes,都不敢回答了:)
刚建立时控件是没有名字的啊
不是可以在程序中直接写AComponent.Name := XXX
自己做一个全局量用来记数,然后用AComponent.Name := 'Edit'+IntToStr(AValue)
再用FindComponent找这个名字的控件找着的话就把AValue+1再试一下:)


 
太烦. 万一设计时改变了name 第二次打开时怎么办? 还要判断?
TTabSheet肯定不是这么做的.
 
待俺看看TTabSheet的源码
 
有点儿冷
 
>> 万一设计时改变了name 第二次打开时怎么办? 还要判断?
存储时名字会被自动存好且下次Component的Load时会自己读出NAME属性值的啊

 
还有我觉得Edit的Owner设成Panel要比设成Form要方便啊
 
那可不行. owner设成panel设计时就无法选中edit了.
 
Another_eYes:
你注意到了吗?
PageControl是用设计期间的弹出彩单来添加TabSheet的,

所以,我的思路:
1、自己写一个TComponentEditor
注册你自己的组件的编辑器,没问题吧?
2、在某个Verb中创建TEdit

当然不是直接用TEdit.Create,
用ComponentEditor的Designer属性(IFormDesigner),
IFormDesigner有CreateComponent方法,如下:
<font color=red>
CreateComponent</font>(ComponentClass:TComponentClass;
Parent:TComponent;
Left,Top,Width,Height:Integer):TComponent;


以上方法我没试过,太累,仅供参考,
不过我觉得可能性很大哦!
 
嗯,用静态的方法好像有问题。在TEdPanel的Create中创建的控件
如果有名字假定为Edit1 ,这个Edit1 就会自动添加到Form的控件
列表中。保存这个程序,在IDE 中再打开,在TEdPanel的Create时
就会提示这个Edit1 已经存在。

我试了TPageControl,它是动态创建TTabSheet 的。如果把TTabSheet
的名字设成空(这时候就象是eyes的TEdPanel静态创建的TEdit 一
样),运行就会出错。

看来,把TEdPanel 做成可以动态添加TEdit 的比较好。
 
对呀!我不就是说写一个ComponentEditor么,

Edit最终是Form中的组件,Panel只负责管理它
Edit也要改造,增加一个Panel属性,
在SetPanel方法中将它自己的引用加到Panel的EditList中去,
应该就差不多了

PageControl和TabSheet好象就是这种关系
 
procedure TForm1.Button1Click(Sender: TObject);
var
demotab:TTabsheet;
begin
demotab:=ttabsheet.Create(Self);
demotab.pagecontrol:=pagecontrol1;
ShowMessage(demotab.Name);
end;
//上面代码加a tabsheet to pagecontrol,是没名字的。
//名字是ide在design时加的
 
hubdog说得对,
在各构件的名字不是自已自动加的,是delphi给加的,帮助中也是这么说明的:
TComponent.Name
Use Name to change the name of a component to reflect its purpose in
the current application. By default, Delphi assigns sequential names
based on the type of the component, such as 'Button1', 'Button2', and
so on.



 
呵呵, tabsheet/pagecontrol是用的tstrings和tlist做到的, 而且是动态产生的,
不是静态的. 和你的case不一样:)

 
我还是觉得应该用<B>IFormDesigner的CreateComponent方法</B>
可是没空,有人去试一下吗?
 
iformdesigner只能在componenteditor,propertyeditor中调用。
我想到一个蠢办法,等我晚上回来在试一下。
 
to eyes:
不行,你在create edit时aowner=form,倒是可编辑了,但form会把你生
成的edit1记录到dfm中,如果你改了name,project存盘,下次你load project ,又会新建3个no name edit,不信你view form as text.这时有6个edit.我想还是用
componenteditor,or use create(self) replace create(aowner)
 
hubdog:
我就是说自己写一个ComponentEditor,然后用Designer
你看前面的帖子
另外,我看PageControl也是这么干的,
你们没人相信么?

我没时间实验,
谁来试试?或者告诉我,为什么不行
 
/// 问题解决了

unit nousage;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls,ExtCtrls,DsgnIntf;

type
TNoUsage = class(TPanel)
private
{ Private declarations }
fedit1: TEdit;
fedit2: TEdit;
fedit3: TEdit;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;

published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TNoUsage]);
end;

constructor TNoUsage.Create(AOwner: TComponent);
var
ownFormDesign:IFormDesigner;
begin
inherited Create(AOwner); // 假定Aowner是form //应该换更准确的办法。
controlstyle := controlstyle - [csAcceptsControls];
if (AOwner<>nil) then
ownFormDesign:=IFormDesigner((AOwner as TForm).Designer);
if (csDesigning in self.ComponentState) and (ownFormDesign<>nil) then
begin
fedit1:=ownFormDesign.CreateComponent(TEdit, Aowner, 5, 5, 100, 12) as TEdit; // 没办法用self要出错的,self还没建立
fedit2:=ownFormDesign.CreateComponent(TEdit, Aowner, 5, 35, 100, 12) as TEdit;
fedit3:=ownFormDesign.CreateComponent(TEdit, Aowner, 5, 65, 100, 12) as TEdit;
end
else
begin
fedit1 := tedit.create(aowner);
fedit1.left := 5;
fedit1.top := 5;
fedit1.text:=aowner.classname;
fedit2 := tedit.create(aowner);
fedit2.left := 5;
fedit2.top := 35;
fedit3 := tedit.create(aowner);
fedit3.left := 5;
fedit3.top := 65;
end;
fedit1.parent := self;
fedit2.parent := self;
fedit3.parent := self;
end;

end.
 

Similar threads

I
回复
0
查看
672
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
577
import
I
顶部