将变量传递给另一个表单 [英] Passing the variable to another Form

查看:109
本文介绍了将变量传递给另一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一些变量的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







<$ p单位Unit1; $ p>

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框,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);
程序Button2Click(发件人:TObject);
procedure Edit4Click(Sender:TObject);
程序Button3Click(发件人:TObject);
procedure Edit1Click(Sender:TObject);
程序Button4Click(发件人:TObject);

private
{私有声明}
public
{公开声明}
idList,imieList,nazwiskoList,adresList:TStringList;
结束

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

实现

使用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');
重置(tempPlik);
i:= 0;
While Not Eof(tempPlik)do
begin
Readln(tempPlik,linia_video [i]);
inc(i);
结束

CloseFile(tempPlik);
结束



程序TForm1.Button4Click(发件人:TObject);
begin
//Form1.Hide;
Form3.Show;
结束

结束。



unit Unit3;

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,窗体,
对话框,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
{私人声明}
public
{公开声明}
end;

var
Form3:TForm3;

实现

使用Unit1;

{$ R * .dfm}

程序TForm3.Button1Click(发件人:TObject);
begin
Form3.Hide;
//Form1.Show;
结束



程序TForm3.FormShow(发件人:TObject);
begin

Label4.Caption:= intToStr(idList.Count);
结束

结束。


解决方案

(我将假设每个表单都在自己的单位。)首先,您必须确保其他单位可以访问 idList 。例如,

  unit Unit1; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框;

type
TForm1 = class(TForm)
private
{私有声明}
idList:integer;
public
{公开声明}
end;

不会,但



<$ p $单位Unit1;

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框;

type
TForm1 = class(TForm)
private
{私有声明}
public
{公开声明}
idList : 整数;
结束

可以。在这种情况下,您需要在 Unit2 中执行的所有操作是将 Unit1 添加到其使用列表(按Alt + F11,或使用文件/'使用单位...',而在 Unit2 或编辑 Form2 时)。然后,您可以使用 Form1.idList Unit2 中的任何位置访问变量。 ( Form1 Unit1 中的全局实例变量 TForm1



例如,

  unit Unit2; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,窗体,
对话框,StdCtrls;

type
TForm2 = class(TForm)
Button1:TButton;
procedure Button1Click(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

var
Form2:TForm2;

实现

使用Unit1; //< - 手动添加,或按Alt + F11(或使用文件/'使用单位...')

{$ R * .dfm}

程序TForm2.Button1Click(发件人:TObject);
begin
ShowMessage(IntToStr(Form1.idList));
结束

结束。


Hello I got Form1 with some variables and I want to pass it to another Form3 where I'll use it. So I have two question.

  • How can I get access to variable in another form? I suppose it will be similar to

    var newIdList:= Form1.idList

  • When var idList get value in

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

    and I show new form in another can I still get value in 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.

解决方案

(I will assume that each form resides in its own unit.) First, you have to make sure that idList is accessible to other units. For example,

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    idList: integer;
  public
    { Public declarations }
  end;

will not do, but

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    idList: integer;
  end;

is OK. In such case, all you need to do in Unit2 is to add Unit1 to its 'uses list' (press Alt+F11, or use File/'Use Unit...', while in Unit2 or while editing Form2). Then you can use Form1.idList to access the variable anywhere inside Unit2. (Form1 is the global instance variable of TForm1 in Unit1).

For example,

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;     // <-- Add manually, or press Alt+F11 (or use File/'Use Unit...')

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(Form1.idList));
end;

end.

这篇关于将变量传递给另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆