to energy:
我把你的程序稍改了一下
unit nousage;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls,ExtCtrls,Dsgnintf;
type
TNoUsage = class(TPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
fedit1: TEdit;
fedit2: TEdit;
fedit3: TEdit;
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);
controlstyle := controlstyle - [csAcceptsControls];
if (AOwner<>nil) then // 假定Aowner是form //应该换更准确的办法。目前没问题
ownFormDesign:=IFormDesigner((AOwner as TForm).Designer);
if not (csLoading in AOwner.ComponentState) then // form 在load的时候不建立
begin
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(self);{有问题}
fedit1.left := 5;
fedit1.top := 5;
fedit2 := tedit.create(self);
fedit2.left := 5;
fedit2.top := 35;
fedit3 := tedit.create(self);
fedit3.left := 5;
fedit3.top := 65;
end;
fedit1.parent := self;
fedit2.parent := self;
fedit3.parent := self;
end;
end;
end.
//主程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,nousage;
type
TForm1 = class(TForm)
fgsdfg: TEdit;
fsgdfg: TEdit;
fghfghf: TEdit;
NoUsage1: TNoUsage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
nousage1.fedit1.text:='a';
end;
end.
//dfm
object Form1: TForm1
Left = 192
Top = 164
Width = 435
Height = 300
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object NoUsage1: TNoUsage
Left = 0
Top = 0
Width = 241
Height = 185
Caption = 'NoUsage1'
TabOrder = 0
object fgsdfg: TEdit
Left = 8
Top = 8
Width = 100
Height = 21
TabOrder = 0
Text = 'fgsdfg'
end
object fsgdfg: TEdit
Left = 8
Top = 32
Width = 100
Height = 21
TabOrder = 1
Text = 'fsgdfg'
end
object fghfghf: TEdit
Left = 8
Top = 56
Width = 100
Height = 21
TabOrder = 2
Text = 'fghfghf'
end
end
object Button1: TButton
Left = 176
Top = 224
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end
//步骤1。把nousage放到form上,把edit1,2,3,改名,save project,load and run 你会发现edit1.text没变。
//你会发现fedit1<>fgsdfg,显示在form上的edit.text没变。你实际上还是建了
6个edit,只不过没显示在dfm里,因为no name edit's parent是nousage ,not
form1.