gpt4 book ai didi

delphi - 将变量传递给另一个表单

转载 作者:行者123 更新时间:2023-12-03 15:43:49 25 4
gpt4 key购买 nike

我得到了带有一些变量的 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.

最佳答案

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

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;

不会,但是

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;

没问题。在这种情况下,您在 Unit2 中所需要做的就是将 Unit1 添加到其“使用列表”(按 Alt+F11,或使用 File/'Use Unit.. .',在 Unit2 中或编辑 Form2 时)。然后,您可以使用 Form1.idList 访问 Unit2 内任何位置的变量。 (Form1Unit1TForm1的全局实例变量)。

例如,

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.

关于delphi - 将变量传递给另一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com