Delphi组件创建......超越一垒

时间:2013-07-31 22:24:10

标签: delphi components creation

我使用VBto作为起点加上对Delphi 6用户指南的大量研究。我可以让我的新组件编译,但我无法找到一种方法来显示它,所以我可以完成它的调试。 50年的编程经验并没有帮助。以下是我的组件的内容:

type 

 TChangeEvent = procedure(Sender: TObject; v: String) of object;
 TTxtSpnr = class(TWinControl)
   Lbl: TLabel;
   Txt: TEdit;
   Scrll:   TScrollBar;
 private
   FonChange: TChangeEvent;
   busy, tweaked: Boolean;
   NewValue: String;
 protected
   procedure Changed(v: String); dynamic;
   property onChange: TChangeEvent read FonChange write FOnChange;
   procedure ScrllChange(Sender: TObject);
   procedure ScrllScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
   procedure TxtEnter(Sender: TObject);
   procedure TxtKeyUp(Sender: TObject; var Key: WORD; Shift: TShiftState);
   procedure TxtExit(Sender: TObject);
   procedure Txt_Validate(var Cancel: Boolean);
 public
   function GetCaption(): String;
   procedure SetCaption(New_Caption: String);
   function GetMax(): Smallint;
   procedure SetMax(New_Max: Smallint);
   function MaxOf(a: Double; B: Longint): OleVariant;
   function MinOf(a: OleVariant; B: Longint): OleVariant;
   function GetMin(): Smallint;
   procedure SetMin(New_Min: Smallint);
   function GetText(): String;
   procedure SetText(New_Text: String);
   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;
 published
   property Caption: String read GetCaption write SetCaption;
   property Enabled: Boolean read GetEnabled write SetEnabled;
   property Max: Smallint read GetMax write SetMax;
   property Min: Smallint read GetMin write SetMin;
   property Text: String read GetText write SetText;
 end;
var 
 TxtSpnr: TTxtSpnr;
implementation 

uses Math; 
{$R *.dfm} 

procedure TTxtSpnr.Changed(V: String); begin 

 if assigned(FonChange) then FonChange(self,V);
end; 

constructor TTxtSpnr.Create(AOwner: TComponent); begin 

 inherited Create(AOwner);
 Lbl := TLabel.Create(Self);
 with Lbl do begin
   Parent := Self;
 end;
 Txt := TEdit.Create(Self);
 with Txt do begin
   Parent := Self;
 end;
 Scrll := TScrollBar(Self);
 with Scrll do begin
   Parent := Self;
 end;
end; 

这是测试驱动程序:

type 

 TForm1 = class(TForm)
   FTxtSpnr: TTxtSpnr;
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
var 

 Form1: TForm1;
implementation 

{$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); begin 

 FTxtSpnr := TTxtSpnr.create(Self);
 with FTxtSpnr do begin
   Left:=10;
   Top:=10;
   Visible:=true;
   Show;
 end;
end; 

但它没有编译并在构造函数中说,#34;一个对象不能成为它自己的父对象#34;。取出父设置,它会编译但不显示组件。我错过了什么?

1 个答案:

答案 0 :(得分:4)

首先,

Scrll := TScrollBar(Self);

当然应该阅读

Scrll := TScrollBar.Create(Self);

其次,

FTxtSpnr := TTxtSpnr.create(Self);
with FTxtSpnr do begin
  Left:=10;
  Top:=10;
  Visible:=true;
  Show;
end;

应该是

FTxtSpnr := TTxtSpnr.create(Self);
with FTxtSpnr do
begin
  Parent := Self;
  Left := 10;
  Top := 10;
end;

您忘了设置父级。

此外,全局变量

var 
  TxtSpnr: TTxtSpnr;

看起来很危险。如果你不确切地知道为什么要添加这两行,你应该删除它们。