写控件的问题(100)

  • 主题发起人 主题发起人 liuguilg
  • 开始时间 开始时间
L

liuguilg

Unregistered / Unconfirmed
GUEST, unregistred user!
我想写一个控件,panel的上方放一个edit(就像LabeledEdit1那样), 下面是我的代码, 加载后panel的上方没有edit,窗体上只有panel ?unit My_panel_edit;interfaceuses SysUtils, Classes, Controls, ExtCtrls, StdCtrls;type TMy_panel_edit = class(TPanel) private { Private declarations } FEdit:TEdit; protected { Protected declarations } public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } end;procedure Register;implementationprocedure Register;begin RegisterComponents('Standard', [TMy_panel_edit]);end;{ TMy_panel_edit }constructor TMy_panel_edit.Create(AOwner: TComponent);begin inherited Create(AOwner); width := 100; height := 100; FEdit := Tedit.Create(self); FEdit.Left := left; FEdit.Top := top-10;//放在距离panel上方10的地方 FEdit.Width := 50; FEdit.Height := 20;end;destructor TMy_panel_edit.Destroy;begin FEdit.Free; inherited Destroy;end;end.
 
constructor TMy_panel_edit.Create(AOwner: TComponent);begin inherited Create(AOwner); width := 100; height := 100; FEdit := Tedit.Create(self); FEdit.Left := left; FEdit.Top := 10;// FEdit.Width := 50; FEdit.Height := 20; Fedit.parent:=self;//end;
 
这样的结果是 Edit是在panel上面,也就是edit的父控件是panel,我想要的是Edit与panel是平级的,比如父控都是form
 
unit My_panel_edit;interfaceuses SysUtils, Classes, Controls, ExtCtrls, StdCtrls;type TMy_panel_edit = class(TPanel) private { Private declarations } FEdit:TEdit; protected { Protected declarations } procedure SetParent(AParent: TWinControl); override; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Edit:Tedit read FEdit; procedure paint;override; published { Published declarations } end;procedure Register;implementationprocedure Register;begin RegisterComponents('Standard', [TMy_panel_edit]);end;{ TMy_panel_edit }constructor TMy_panel_edit.Create(AOwner: TComponent);begin inherited Create(AOwner); width := 400; height := 200; top:=100; left:=100; FEdit := Tedit.Create(self); FEdit.Left := left; FEdit.Top := top-10;//放在距离panel上方的地方 FEdit.Width := 50; FEdit.Height := 20;end;destructor TMy_panel_edit.Destroy;begin// FEdit.Free; inherited Destroy;end;procedure TMy_panel_edit.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation); if (AComponent = FEdit) and (Operation = opRemove) then FEdit := nil;end;procedure TMy_panel_edit.paint;begin inherited; caption:='* panle的parnet是'+Parent.Name+' '+'edit的parent是'+fedit.Parent.Name;end;procedure TMy_panel_edit.SetParent(AParent: TWinControl);begin inherited SetParent(AParent); if FEdit = nil then exit; FEdit.Parent := AParent; FEdit.Visible := True;end;end.procedure TForm1.Button1Click(Sender: TObject);begin with TMy_panel_edit.Create(self) do begin Parent:=self; end;end;
 
可以了,多谢
 

Similar threads

后退
顶部