(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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…