在Form2的Public区域定义一个变量,比如:
a:Integer;
然后,在创建后赋值:
Form2:=TForm2.Create(self);
Form2.a := 100;
注意,显示这个参数的值不能在FormCreate事件中,可以在FormShow中
Showmessage(IntToStr(a));
a:Integer;
然后,在创建后赋值:
Form2:=TForm2.Create(self);
Form2.a := 100;
注意,显示这个参数的值不能在FormCreate事件中,可以在FormShow中
Showmessage(IntToStr(a));
解决方案 »
- fastreport在设计期怎样得到数据集以外数据?能不能自定义全局的变量?
- 【★★★ToolBar &ToolBotton Borland 的工程师为什么要这样写?★★★】
- 还是老问题!(fengzhengren来看一下,我用的是你的方法!)
- 如何取消WebBrowser控件的滚动条啊?谢谢:)
- 一个货品有多个单位如何处理
- treeview控件的问题!高分相赠!!!
- 求教个SQL语句
- 把不规则文本导入oracle数据库的最好方法!极具挑战性!
- Image背景颜色
- web中用BDE连接数据库的冲突
- 再问:200分!!! 关于 OleContainer 的问题:我用OleContainer内嵌了一个Excel Worksheet到程序中,可是我不知道如何在程序中控制它....
- 十万火急:人生之路如何走:证书??+如何击败强劲对手?
通常会在IDE中创建应用程序的窗体,以这种方式创建窗体,窗体就会拥有一个只包含一个参数Owener的构造函数,要将其它参数传递给窗体,需要另外的构造函数。
待续:)
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;
小小补充:
constructor Create(Sender:TComponent;intIni:integer);
这一行在编译时会有一个警告,可以修改为
constructor Create(Sender:TComponent;intIni:integer); reintroduce;
来把它压制掉。
form2.aVal:=form1.aVal或者用全局变量不行。这是存在和可见、传递和引用的之乎者也。
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;总之这样赋值的方法很多,需要根据你自己的需要选择。
如何在只知道窗体的类名的情况下,创建窗体?
或者说,我定义了一个窗体的类TForm2, 并在其中加入了很多控件,如何在只有Form2这个字符串的情况下,创建Tform2的实例?
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;