急呀,请教一下,窗体间如何进行变量值的传递????
powre 2017-09-15 05:44:06 唉,初学者就是心累,对于大神们来说很简单的问题,我这里一卡就是一天。还是登录界面的值传递到另一个窗体的问题,要把Form1里面的帐号和权限传递到Form3窗体(Form2是模块类),请问如何实现?
我查过书,也百度过,方法就是在Form1中定义一个记录,在Form1的uses中加入Unit3,在Form3中的uses中加入Unit1,然后
Form3.show后,在Form3窗体中调用出来。代码如下,有劳各位大神费心了!
Form1代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
UserInfo = record
UserName: string;
UserPassword:string;
UserRole:string;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Auser:UserInfo;
end;
var
Form1: TForm1;
ActionCD:string;
implementation
uses Unit3, Unit2;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if(Trim(Edit1.Text)='') then
begin
showMessage('请输入帐号');
Edit1.SetFocus();
exit;
end;
if(Trim(Edit2.Text)='') then
begin
showMessage('请输入密码');
Edit2.SetFocus();
exit;
end;
with DataModule2.ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from users where UserName=:a and Password=:b'); //查找帐号密码
Parameters.ParamByName('a').Value:=Edit1.Text;
Parameters.ParamByName('b').Value:=Edit2.Text;
Open;
if RecordCount=1 then
begin
showMessage('登录成功!');
{ showMessage('你的帐号是:'+FieldByName('username').AsString +
'你的密码是:'+FieldByName('password').AsString +
'你的权限是:'+FieldByName('role').AsString);
UserInfo:=TUserInfo.Create;
UserInfo.UserName:= FieldByName('username').AsString;
UserInfo.UserPassword:=FieldByName('password').AsString;
UserInfo.UserRole:=FieldByName('role').AsString;
}
Auser.UserName:= FieldByName('username').AsString;
Auser.UserPassword:= FieldByName('password').AsString;
Auser.UserRole:= FieldByName('role').AsString;
showMessage(Auser.UserRole);
Form3.Show;
end
else
// showMessage('帐号或密码错误,请重新输入!');
MessageBox(handle,'用户名或密码有误,请重试!','非法用户',MB_ICONSTOP+mb_ok);
Edit1.Text:='';
Edit2.Text:='';
exit;
end;
end;
end.
Form3(传递变量的结果):
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
uses Unit1,Unit2;
procedure TForm3.FormCreate(Sender: TObject);
begin
Label1.Caption:='你的帐号是:'+ Form1.Auser.UserName;
end;
end.
结果就是显示不出来,我查了好久没发现问题,请教一下各位大神,如何实现?谢谢了!