Home > Back-end >  Ah, I ask, how to pass a variable's value between form??????
Ah, I ask, how to pass a variable's value between form??????

Time:09-17

Alas, for beginners is the heart tired, very simple problem for the great god, I here a card is a day, or the value of the login screen to another form, to pass Form1 inside account and permissions to Form3 form (Form2 module class), could you tell me how to implement?
I checked the book, baidu, also is to define a record in the Form1, adding Unit3 in Form1 USES in Form3 USES to add Unit1, then
Form3. After the show, called out in the Form3 form, the HTML code is as follows, please everyone a great god bother!

Form1 code:
The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

Type
The UserInfo=record
UserName: string;
UserPassword: string;
UserRole: string;
The 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: the UserInfo;

The end;

Var
Form1: TForm1;
ActionCD: string;

Implementation

USES Unit3, Unit2;

{$R *. DFM}

Procedure TForm1. BitBtn1Click (Sender: TObject);
The begin

If (Trim (Edit1. Text)=' ') then
The begin
ShowMessage (' please enter the account ');
Edit1. SetFocus ();
exit;
The end;

If (Trim (Edit2. Text)=' ') then
The begin
ShowMessage (' please enter the password);
Edit2. SetFocus ();
exit;
The end;

With DataModule2. ADOQuery1 do
The begin
The Close;
SQL. The Clear;
SQL. The Add (' select * from the users where the UserName=: and Password=a: b ');
//find the account passwordThe Parameters. ParamByName (' a '). The Value:=Edit1. Text;
The Parameters. ParamByName (' b '). The Value:=Edit2. Text;
The Open;

If RecordCount=1 then
The begin
ShowMessage (' login successfully! ');

{showMessage (' your account number is: + FieldByName (" username "). The AsString +
'your password is:' + FieldByName (" password "). The AsString +
'your permissions are:' + FieldByName (' role '). AsString) are identical.



The UserInfo:=TUserInfo. Create;
The UserInfo. UserName:=FieldByName (" UserName "). AsString;
The UserInfo. UserPassword:=FieldByName (" password "). AsString;
The 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
The else
//showMessage (' account or password error, please input again! ');
MessageBox (handle, 'the user name or password is wrong, please try again! ', 'illegal users' MB_ICONSTOP + mb_ok);
Edit1. Text:=' ';
Edit2. Text:=' ';
exit;
The end;

The end;


End.



Form3 (transfer variable results) :

The unit Unit3.

Interface

USES the
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}
The end;

Var
Form3: TForm3;

Implementation

{$R *. DFM}
USES Unit1, Unit2;

Procedure TForm3. FormCreate (Sender: TObject);
The begin
Label1. Caption:='your account was:' + Form1. Auser. UserName;
The end;

End.

The result is showed not to come out, I checked haven't found the problem, I ask you a great god, and how to do? Thank you very much!

CodePudding user response:

TForm3 class add a SetUserName members of the public methods and FUserName
Procedure TForm3: : SetUserName (username: string)
The begin
FUserName=username;
The end;
After created the object, call the method, can preach in the past, after visiting FUserName,

CodePudding user response:

Can still don't know much about, for example?

CodePudding user response:

Reading is the absolute principle, such a problem, let a person can't laid hands on him,

Form between variables, exchange, can pass:
Unit 1, USES to join each other to solve,
2, the two sides Shared global variables,
3, by specifying unit changes or events, such as Form1. Button

CodePudding user response:

Not really, can provide a simple example file?

CodePudding user response:

Refer to another FORM, can be directly transfer

CodePudding user response:

I think you can't put in procedure TForm3. FormCreate (Sender: TObject); Can't find the variable, create status is wrong, but should be on procedure TForm3. FormActivate (Sender: TObject); After creation, form variables can be found, you give it a try,

CodePudding user response:

Of course, in the procedure TForm3. FormShow (Sender: TObject); Also can, ha ha

CodePudding user response:

A Global increase. Pas unit file, put the information here (var) in the interface, then Unit1, Unit2, in the implementation of Unit3 USES Global. The pas

CodePudding user response:

Form3 write a global function is used to pass by value, in Form1 call can be

CodePudding user response:

Usually projects, the establishment of a global variable file, global constants file, file sharing function, and the catch-all category for each of these common processing, other places need reference file use can be directly,

CodePudding user response:

references to the tenth floor tanqth response:
usually projects, build a global variable file, global constants file, file sharing function, and the catch-all category for each of these common processing, other places need reference file use can directly,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnull