关于调用unit的问题! |
尚未結案
|
lhh
一般會員 發表:16 回覆:21 積分:7 註冊:2004-11-14 發送簡訊給我 |
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation
uses
unit3;
{$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
aa : Tclassreference;
bb : Tcontrol;
begin
aa.Create(button1);
bb.Create(button1);
aa.controlcreate(bb, 'Buttonx', 10, 10, 100, 20);
end; end. unit Unit3; interface
uses
Controls, Forms,StdCtrls,Classes; type
Tclassreference = class(Tcomponent)
public
constructor create; overload ;
function controlcreate(Control :Tcontrol; const controlname:string; X,Y,W,H :Integer) : Tcontrol;
end; implementation { Tclassreference } function Tclassreference.controlcreate(Control: Tcontrol;
const controlname: string; X, Y, W, H: Integer): Tcontrol;
begin
result := control.Create(nil); result.name := controlname;
end; constructor Tclassreference.create;
begin
inherited create(self);
end; end. 我想尝试一下在unit3中建立一条function,然后在unit1中调用它。但是delphi说:
[Warning] Unit1.pas(33): Variable 'aa' might not have been
initialized。
[Warning] Unit1.pas(34): Variable 'bb' might not have been initialized。 请各位高手帮我修改修改!!谢谢!!
|
Zard
尊榮會員 發表:24 回覆:396 積分:539 註冊:2003-11-26 發送簡訊給我 |
|
chris_shieh
高階會員 發表:46 回覆:308 積分:240 註冊:2004-04-26 發送簡訊給我 |
procedure TForm1.Button1Click(Sender: TObject); var aa : Tclassreference; bb : Tcontrol; begin aa := Tclassreference.Create(button1); bb := Tcontrol.Create(button1); bb已經指向button1 instance aa.controlcreate(bb, 'Buttonx', 10, 10, 100, 20); controlcreate回傳之Tcontrol並未給回bb end;我猜想你是想要按下button1時動態改變其 名稱、大小、位置、 建議以下作法: Unit3中的controlCreate 宣告改為 procedure controlcreate(var Control :Tcontrol; const controlname:string; X,Y,W,H :Integer)實做內容改為: procedure Tclassreference.controlcreate(var Control: Tcontrol; const controlname: string; X, Y, W, H: Integer); begin if Assigned(Control) then begin Control.name := controlname; Control.Left:=X; Control.Top:=Y; Control.Width:=W; Control.Height:=H; end; end;則Unit1 可以採用下面方式改變 aa := Tclassreference.Create(button1); aa.controlcreate(button1, 'Buttonx', 10, 10, 100, 20); aa.Free;不過如果只是要做到這樣動作,應該是可以不需要Tclassreference 直接宣告controlcreate就可以 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |