将变量传递给另一个表单

时间:2011-12-18 12:42:47

标签: delphi

你好我得到了Form1的一些变量,我想将它传递给另一个我将使用它的Form3。所以我有两个问题。

  • 如何以其他形式访问变量?我想它会 类似于

    var newIdList:= Form1.idList

  • 当var idList在

    中获取值时

    procedure TForm1.Button1Click(Sender: TObject);begin idList:=strtoint(edit1.text); end

    我在另一个中显示新表格我仍然可以在idList获得价值吗?

    procedure TForm1.Button2Click(Sender: TObject); begin form1.hide; form3.show; end


    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        PageControl1: TPageControl;
        TabSheet1: TTabSheet;
        TabSheet2: TTabSheet;
        TabSheet3: TTabSheet;
        Label5: TLabel;
        Edit3: TEdit;
        Edit2: TEdit;
        Button3: TButton;
        Edit4: TEdit;
        Button2: TButton;
        Button1: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        Label3: TLabel;
        Label2: TLabel;
        Edit5: TEdit;
        Label7: TLabel;
        Label6: TLabel;
        Button4: TButton;
        ListBox1: TListBox;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Edit4Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Edit1Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);

      private
        { Private declarations }
      public
        { Public declarations }
        idList,imieList,nazwiskoList,adresList: TStringList;
      end;

    var
      Form1: TForm1;
      plik:TStringList;
      tempPlik:TextFile;
      st:string;
      linia_klient,linia_video:array[0..20] of string;
      id:integer;

    implementation

    uses Unit3;

    {$R *.dfm}

    .
    .
    .

    procedure TForm1.FormCreate(Sender: TObject);
    var i:integer;
    begin
    Edit1.Text:='Witaj, Podaj ID klienta';
    Label1.Caption:='ID Klienta';
    idList:=TStringList.Create;
    imieList:=TStringList.Create;
    nazwiskoList:=TStringList.Create;
    adresList:=TStringList.Create;

    if (FileExists('idList.txt')=true) then idList.LoadFromFile('idList.txt') else idList.SaveToFile('idList.txt');
    if (FileExists('imieList.txt')=true) then imieList.LoadFromFile('imieList.txt') else imieList.SaveToFile('imieList.txt');
    if (FileExists('nazwiskoList.txt')=true) then nazwiskoList.LoadFromFile('nazwiskoList.txt') else nazwiskoList.SaveToFile('nazwiskoList.txt');
    if (FileExists('adresList.txt')=true) then adresList.LoadFromFile('adresList.txt') else adresList.SaveToFile('adresList.txt');

    AssignFile(tempPlik,'video.txt');
    Reset(tempPlik);
    i:=0;
    While Not Eof(tempPlik) do
      begin
        Readln(tempPlik,linia_video[i]);
        inc(i);
      end;

    CloseFile(tempPlik);
    end;



    procedure TForm1.Button4Click(Sender: TObject);
    begin
      //Form1.Hide;
      Form3.Show;
    end;

    end.



unit Unit3;

interface

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

type
  TForm3 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
Form3.Hide;
//Form1.Show;
end;



procedure TForm3.FormShow(Sender: TObject);
begin

Label4.Caption:= intToStr(idList.Count);
end;

end.

1 个答案:

答案 0 :(得分:1)

'如何以其他形式访问变量?' - 是的,只要变量是公共或已发布的成员并且您可以访问实例变量,就可以像访问任何其他类实例变量一样访问它。通常,这意味着将包含'Form1'类的单元添加到需要访问的单元的uses子句中。

'我在另一个中显示新形式我还能在idList中获得价值吗?' - 当然,只要表格存在,您就可以访问表单实例变量,并且'idList'是公共的或已发布的。