Access to the properties of visual components in a form is not considered good practice. It is considered better to have separate properties. In the above example, you have username and password properties with get and set methods.
For instance:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; private function GetPassword: string; function GetUsername: string; procedure SetPassword(const Value: string); procedure SetUsername(const Value: string); public property Password: string read GetPassword write SetPassword; property Username: string read GetUsername write SetUsername; end; var Form1: TForm1; implementation {$R *.dfm} function TForm1.GetPassword: string; begin Result := Edit2.Text; end; function TForm1.GetUsername: string; begin Result := Edit1.Text; end; procedure TForm1.SetPassword(const Value: string); begin Edit2.Text := Value; end; procedure TForm1.SetUsername(const Value: string); begin Edit1.Text := Value; end; end.
This means that you can change the visual components of the form without affecting the calling code.
Another option is to pass the object as a property in the dialog box;
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TUserObject = class(TObject) private FPassword: string; FUsername: string; public property Password: string read FPassword write FPassword; property Username: string read FUsername write FUsername; end; TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; btnOK: TButton; procedure btnOKClick(Sender: TObject); private FUserObject: TUserObject; procedure SetUserObject(const Value: Integer); public property UserObject: Integer read FUserObject write SetUserObject; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btnOKClick(Sender: TObject); begin FUserObject.Username := Edit1.Text; FUserObject.Password := Edit2.Text; ModalResult := mrOK; end; procedure TForm1.SetUserObject(const Value: Integer); begin FUserObject := Value; Edit1.Text := FUserObject.Username; Edit2.Text := FUserObject.Password; end; end.
Hope this helps.
source share