动态创建窗体时如何传递参数?

ypyRock 2001-10-04 07:33:24
动态创建一个窗体时如何传递一个参数过去?就是a创建b的时候传一个参数给b,b接受参数
在form1里创建
Form2:=TForm2.Create(self);
//怎么传一个参数?

在form2里
TForm2.onCreate(..)
begin
showmessage(参数);
//怎么接受参数?
end;
...全文
427 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
TechnoFantasy 2001-11-09
  • 打赏
  • 举报
回复
type TControlClass = class of TControl;
function CreateControl(ControlClass: TControlClass;

const ControlName: string; X, Y, W, H: Integer): TControl;
begin
Result := ControlClass.Create(MainForm);
with Result do
begin
Parent := MainForm;
Name := ControlName;
SetBounds(X, Y, W, H);
Visible := True;
end;
end;
baniu 2001-11-09
  • 打赏
  • 举报
回复
多多关注
yaos 2001-10-20
  • 打赏
  • 举报
回复
请教!
如何在只知道窗体的类名的情况下,创建窗体?
或者说,我定义了一个窗体的类TForm2, 并在其中加入了很多控件,如何在只有Form2这个字符串的情况下,创建Tform2的实例?
movingboy 2001-10-05
  • 打赏
  • 举报
回复
同意TechnoFantasy说的,这是比较好的解决方案。
小小补充:
constructor Create(Sender:TComponent;intIni:integer);
这一行在编译时会有一个警告,可以修改为
constructor Create(Sender:TComponent;intIni:integer); reintroduce;
来把它压制掉。
TechnoFantasy 2001-10-05
  • 打赏
  • 举报
回复
另外一个方法就是使用属性来解决:

type
TForm1 = class(TForm)
private
{ Private declarations }
myInt:integer;
procedure SetAInt(const Value: integer);
public
{ Public declarations }
property AInt :integer read myInt write SetAInt;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.SetAInt(const Value: integer);
begin
myInt:=Value;
end;

总之这样赋值的方法很多,需要根据你自己的需要选择。
sundayboys 2001-10-05
  • 打赏
  • 举报
回复
一觉醒来,TechnoFantasy(www.applevb.com)老兄把我的下文说了:))
scripit 2001-10-05
  • 打赏
  • 举报
回复
转悠?
form2.aVal:=form1.aVal或者用全局变量不行。这是存在和可见、传递和引用的之乎者也。
ypyRock 2001-10-05
  • 打赏
  • 举报
回复
谢谢诸位的帮忙,欢迎大家继续讨论,我想多学习一点
w_anghe 2001-10-05
  • 打赏
  • 举报
回复
我认为没有特殊需要,还是chechy老兄的方法更为实用
TechnoFantasy 2001-10-04
  • 打赏
  • 举报
回复
可以重载窗体的构造函数:

unit Unit2;

interface

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

type
TForm2 = class(TForm)
procedure FormShow(Sender: TObject);
private
{ Private declarations }
intSelf:integer;
public
{ Public declarations }
constructor Create(Sender:TComponent;intIni:integer);
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

constructor TForm2.Create(Sender: TComponent; intIni: integer);
begin
inherited Create(Sender);
intSelf := intIni;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
showmessage(inttostr(intSelf));
end;

end.

上面的代码重载了构造函数。添加了一个参数intIni,在构造函数中将参数intIni保存
到私有变量intSelf中。然后在Show事件中显示消息框显示intSelf的值。调用的范例:

procedure TForm1.Button1Click(Sender: TObject);
begin
with TForm2.Create(Self,100)do
try
ShowModal;
finally
free;
end;
end;
煜知搬砖者 2001-10-04
  • 打赏
  • 举报
回复
等我回到学校告诉你
chechy 2001-10-04
  • 打赏
  • 举报
回复
呵呵,谢谢关心。我的病只是不能长时间站立、走路,否则腿会很痛。其它就没什么。
sundayboys 2001-10-04
  • 打赏
  • 举报
回复
呵呵,老大还没有休息啊:)注意身体哦:)

我个人认为:
通常会在IDE中创建应用程序的窗体,以这种方式创建窗体,窗体就会拥有一个只包含一个参数Owener的构造函数,要将其它参数传递给窗体,需要另外的构造函数。


待续:)
chechy 2001-10-04
  • 打赏
  • 举报
回复
在Form2的Public区域定义一个变量,比如:
a:Integer;
然后,在创建后赋值:
Form2:=TForm2.Create(self);
Form2.a := 100;
注意,显示这个参数的值不能在FormCreate事件中,可以在FormShow中
Showmessage(IntToStr(a));

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧