如果在函数里调用一个form?(50分)

  • 主题发起人 主题发起人 wing_sky
  • 开始时间 开始时间
W

wing_sky

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在一个unit写一个函数,从这个函数里调另一个form;
如:function Name1(FormA :???) String;
begin
with FormA do //调用另一个窗体的资源
begin
.
.
.
end;
end;
请问我这个FormA应该如何定义?在函数调用时,Name1(???)应该如何写?
 
function Name1(AForm:TForm):string;
begin
with AForm do
begin
...
end;
end;
 
在函数中加上assigned(AForm)判断是否建立了,返回True才可以调用其中的资源!
 
如果我在AForm里有个TRadioButton,
function Name1(AForm:TForm):string;
begin
with AForm do
begin
if RadioButton1.Checked then result :='可选';
...
end;
end;
会提示RadioButton1没有声明,请问有没有解决方法?
 
把你的情况说清楚点,这个问题应该没有多难的!
 
从一个表单调用另一个表单?
还是从一个表单调用另一个表单的函数?
 
我的意思是如果在AForm里有个单选按钮RadioButton1,我要在另一个单元里的函数中调用
它,象上面的那样:
with AForm do
begin
if RadioButton1.Checked then result :='可选'; //其中RadioButton1是在AForm里面的!
...
end;
这样可行吗?
 
可以啊,你只要将你需要引用的FORM单元加入到调用的单元就可以,Form可以调用那么它
其中的RadioButton1就可以引出来的,因为RadioButton1在form中是定义在published中的
 
想法完全同这位老兄:[:D][:)]
来自:晶晶, 时间:2003-1-20 0:18:00, ID:1589738
可以啊,你只要将你需要引用的FORM单元加入到调用的单元就可以,Form可以调用那么它
其中的RadioButton1就可以引出来的,因为RadioButton1在form中是定义在published中的


 
晶晶的方法不会有问题,我经常这么做。但是wing_sky的情形不一样。
如果你使用form作为函数的参数,这里面的RadioButton1显然没有定义,因为:
function Name1(AForm:TForm):string;
你这里定义的参数是TForm类的。原始的TForm类怎么会有RadioButton1在上面呢?

如果你的AForm是这样定义的:
var Form2: TForm2;

那么你的函数应该是这样:
function Name1(AForm:TForm2):string;

这样就没问题了
 
to 晶晶
你的这办法好象不行,还是没有找到RadioButton1,我把单元加进去了,可在编译的时候
系统并不知道你的AForm就是指的这个单元啊,如果这样写:Form1.RadioButton1.Checked then result :='可选';
这样虽然行,但就不是我的本意了,因为在调用前我并不知道所调用的Form名;
to seraph_q
请问var Form2: TForm2;中的是什么意思?在form定义的时候好象只
有:var Form2:TForm2;
 
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit3;//unit3在函数定义前就要加入

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function Name1(Aform:TForm3):string;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.Name1(Aform:TForm3):string;
begin
result:=aform.RadioButton1.Caption;//返回值
end;
procedure TForm1.Button1Click(Sender: TObject);
var a:tform3;//先申明
begin
a:=tform3.Create(application);//要先显示建立,因为这样才返回a指针值
showmessage('a.RadioButton1.Caption is: '+name1(a));
end;

end.
unit Unit3;

interface

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

type
TForm3 = class(TForm)
RadioButton1: TRadioButton;//定义好RadioButton1
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;

implementation

{$R *.dfm}

end.
注意函数function TForm1.Name1(Aform:TForm3):string;中的aform一定是一个(有效)指针
才可以访问,不然的话会发生内存访问错误的!依靠var a:tform3;//先申明
begin
a:=tform3.Create(application);//要先显示建立,因为这样才返回a的有效指针值
不知道是不是你要的效果!!!!

 
function Name1(AForm:TForm):string;
begin
with (AForm as Form2) do
begin
if RadioButton1.Checked then result :='可选';
...
end;
end;
这里,假定你的RadioButton1是定义在Form2中。
 
to Lyte Zeng
用你的方法会出现‘perator not applicable to this operand type’的错误;
to 晶晶
如果我是固定的去调那个form,我想也就没有做这个函数的必要了,我是想如果form1,
form2,form3......里都有一个RadioButton1,我要在每个form需要的时候调用这个函数,
而每个form的RadioButton1.Caption都有可能不同, 这样不知道该怎么做?
 
怎么这么做呢?要动态决定哪个Form,那每个Form的定义都不同啊!
你自己定义一个Form结构,都有radiobutton这个元素,然后用AS转换,但那个方法很麻烦,
还不如直接写,因为用那么多方法来转化没有必要啊!
用类引用方法试试,参照Application(TForm1,Form1);参数传递方式和机制!我自己还没有试
过,你先试试,看行不??!!
 
接受答案了.
 
后退
顶部