在Form2的Public区域定义一个变量,比如:
a:Integer;
然后,在创建后赋值:
Form2:=TForm2.Create(self);
Form2.a := 100;
注意,显示这个参数的值不能在FormCreate事件中,可以在FormShow中
Showmessage(IntToStr(a));

解决方案 »

  1.   

    呵呵,老大还没有休息啊:)注意身体哦:)我个人认为:
    通常会在IDE中创建应用程序的窗体,以这种方式创建窗体,窗体就会拥有一个只包含一个参数Owener的构造函数,要将其它参数传递给窗体,需要另外的构造函数。
    待续:)
      

  2.   

    可以重载窗体的构造函数:unit Unit2;interfaceuses
      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;
      

  3.   

    同意TechnoFantasy说的,这是比较好的解决方案。
    小小补充:
        constructor Create(Sender:TComponent;intIni:integer);
    这一行在编译时会有一个警告,可以修改为
        constructor Create(Sender:TComponent;intIni:integer); reintroduce;
    来把它压制掉。
      

  4.   

    我认为没有特殊需要,还是chechy老兄的方法更为实用
      

  5.   

    转悠?
    form2.aVal:=form1.aVal或者用全局变量不行。这是存在和可见、传递和引用的之乎者也。
      

  6.   

    一觉醒来,TechnoFantasy(www.applevb.com)老兄把我的下文说了:))
      

  7.   

    另外一个方法就是使用属性来解决: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;总之这样赋值的方法很多,需要根据你自己的需要选择。
      

  8.   

    请教!
    如何在只知道窗体的类名的情况下,创建窗体?
    或者说,我定义了一个窗体的类TForm2, 并在其中加入了很多控件,如何在只有Form2这个字符串的情况下,创建Tform2的实例?
      

  9.   

    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;