关于FORM的Create重载的问题(100分)

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

lyq81404

Unregistered / Unconfirmed
GUEST, unregistred user!
我最近写了段代码,将FORM的CREATE重载,但是重载的Create函数内的代码却没有执行,我哪里写错了吗?
代码节选:
unit ufrmCostImput;

interface

type
TfrmCostImput=Class(TForm)
Private

public
Constructor Create;reintroduce;overload;
end;

implementation

uses ufrmMain;

constructor TfrmCostImput.create;
begin
inherited Create(application);
frmMain.CanCanChange:=True;
end;
end.
 
声明:constructor Create(AOwner: TComponent)
overload
override;

语句,Form本身的Create是带参数的,你覆盖的是没有参数的Create,当然不会执行
constructor TfrmChild.Create(AOwner: TComponent);
begin
FAAAA:= False;
inherited Create(AOwner);
end;

另外看你的这个窗体引用了主窗体的东西,这样不规范吧?
窗体之间最好不要互相引用,把变量放到公共单元里吧
 
一、一条大鱼的说法没错,但不规范
Constructor Create(AOwner:TCmoponent);在父类中已存在,
现在所要做的是只是覆盖,不需要重载;
因此改为
Constructor Create(AOwner:TCmoponent);override;

Constructor TfrmCostImput.Create(AOwner:TCmoponent);override;
begin
inhreited Create(AOwner);
//这里最好先判断frmMain是否存在,不存在就访问,会有地址访问错误
//如Application并没有创建frmMain,或frmCostInput在frmMain之前创建
//不过,既然命名为frmMain,想必是主窗体
//假设frmMain不是主窗体,那么,如果frmMain为几个窗体所共有,最好在公共单元声明
if not Assigned(frmMain) then
frmMain:=TfrmMain.Create(Application);
//如果为frmCostImput私有,既然是私有,那么最好是这样写
//frmMain:=TfrmMain.Create(Self)

frmMain.CanCanChange:=True;
end;

二、为何没执行?
Constructor Create;reintroduce;overload;
只是重载了Create构造函数,并没覆盖Create(AOwner:TComponent)
而Form在Create时用的是Create(AOwner)函数,并不是Create函数。
如果要用,笨方法倒是有(确实也够笨的。用上面的方法就好了。)
//因为下面有两个create函数,所以要overload
constructor Create(AOwner:TComponent);overload;;override
constructor Create;overload;

constructor TfrmCostImput.Create(AOwner: TComponent);
begin
ShowMessage('before');
create;
// inherited Create(AOwner)
//这一句不能用
ShowMessage('After');
end;

constructor TfrmCostImput.Create;
begin
inherited Create(Application);
if not Assigned(frmMain) then
frmMain:=TfrmMain.Create(Application)

frmMain.CanCanChange:=True;
ShowMessage('here');
end;
 
先感谢两位的帮助,没仔细看FORM的CREATE,呵呵。
我是在主窗口设置了一段私有域,用来定义要用到的一些变量,因为我不知道如何从SHOWMODAL的窗口传递数据给上层窗口,只好用这个笨办法,都是定义的属性,算是全局变量吧。如果有更好的办法,请多赐教。
 
unit Unit1;
......
TfrmMain = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
...
End;

procedure TfrmMain.Button1Click(Sender: TObject);
var
frmModal: TfrmModal;
begin
frmModal:=TfrmModal.Create(nil);
with frmModal do
try
ShowModal;
if ModalResult=mrOK then
ShowMessage(edit1.text)
else
ShowMessage('没找到诶');
finally
free;
end;
end;


unit Unit2

TfrmModal = class(TForm)
BitBtn1: TBitBtn
//Kind设为bkOK,ModalResult设为mrOK
Edit1: TEdit;
BitBtn2: TBitBtn
//Kind设为bkCancel,ModalResult设为mrCancel
......
end;
 
就是说一个窗口SHOWMODEL另一个窗口的时候,在SHOWMODEL窗口关闭之后只要没有这个SHOWMODEL窗口没有释放就可以访问这个SHOWMODEL窗口的数据是吧,晕倒,我有蛮笨的,一个人自己研究,想法还是容易局限,有人点破一下就是好:)
 
多人接受答案了。
 
还有一个问题,就是SHOWMODAL窗口如何访问调用它的窗口里的数据?窗口又如何传递参数给它SHOWMODAL的窗口?头大了.........
 
比方说,我的Mainform动态创建并SHOWMODAL了From1,而FOrm1又动态创建并SHOWModal了From2,然后我在form2里的implementation后面Uses了Form1,接着在FOrm2中用Form1.Value,访问Form1里的属性,通过编译,但运行中出错,错误的内存读写。搞不懂了.........
 
一条大鱼,被你抓了把柄我,我收声。
楼主,写个例子给你(窗体间传递参数):
工程文件:
program ShowModalForm;

uses
Forms,
U_Main in 'U_Main.pas' {f_Main},
U_ModalForm in 'U_ModalForm.pas' {f_ModalForm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(Tf_Main, f_Main);
Application.Run;
end.
---------------------------------------------------
主窗体:
unit U_Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
Tf_Main = class(TForm)
BtnMethodA: TButton;
Edit1: TEdit;
BtnMethodB: TButton;
btnMethodC: TButton;
procedure BtnMethodAClick(Sender: TObject);
procedure BtnMethodBClick(Sender: TObject);
procedure btnMethodCClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
f_Main: Tf_Main;

implementation

uses U_ModalForm;

{$R *.dfm}

procedure Tf_Main.BtnMethodAClick(Sender: TObject);
var
form:Tf_ModalForm;
begin
form:=Tf_ModalForm.Create(nil,Edit1.Text);
with form do
try
ShowModal;
if ModalResult=mrOK then
ShowMessage('答案是: '+edit2.Text);
finally
free;
end;
end;

procedure Tf_Main.BtnMethodBClick(Sender: TObject);
var
strAnswer:String;
begin
if ShowQuestionForm(Edit1.Text,strAnswer) then
ShowMessage('答案是: '+strAnswer);
end;

procedure Tf_Main.btnMethodCClick(Sender: TObject);
var
form:Tf_ModalForm;
begin
form:=Tf_ModalForm.Create(nil);
with form do
try
Edit1.Text:=self.Edit1.Text;
ShowModal;
if ModalResult=mrOK then
ShowMessage('答案是: '+edit2.Text);
finally
free;
end;
end;

end.
------------------------------------------------------
Modal窗体:
unit U_ModalForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
Tf_ModalForm = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
btnGetQuestion: TButton;
procedure btnGetQuestionClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FQuestion:String;
public
{ Public declarations }
constructor Create(AOwner:TComponent;AQuestion:String);overload;
end;

Function ShowQuestionForm(AQuesition:string;var Answer:String):Boolean;
implementation

{$R *.dfm}
Function ShowQuestionForm(AQuesition:string;var Answer:String):Boolean;
var
form:Tf_ModalForm;
begin
Result:=False;
form:=Tf_ModalForm.Create(nil);
with form do
try
Edit1.Text:=AQuesition;
ShowModal;
if ModalResult=mrOk then
begin
Answer:=edit2.text;
Result:=True;
end;
finally
free;
end;
end;
{ Tf_ModalForm }

constructor Tf_ModalForm.Create(AOwner: TComponent
AQuestion: String);
begin
inherited Create(AOwner);
FQuestion:=AQuestion;
end;

procedure Tf_ModalForm.btnGetQuestionClick(Sender: TObject);
begin
Edit1.Text:=FQuestion;
end;

procedure Tf_ModalForm.FormCreate(Sender: TObject);
begin
Caption:=DateTimeToStr(now);
end;

end.

我以前写的例子: http://downloads.2ccc.com/simples/forms/InheritedOverload.rar
 
后退
顶部