Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 获取变量_Delphi_Variables - Fatal编程技术网

Delphi 获取变量

Delphi 获取变量,delphi,variables,Delphi,Variables,我获得了一个关于如何在创建主窗体之前创建登录屏幕的示例。 然而,我不知道如何在登录屏幕关闭之前获取变量。我正在尝试传递变量 SelectedUserName : String; SelectedUserIdNo, SelectedCoyId : Integer; 从LoginForm到mainform进行进一步处理 任何想法 提前谢谢 以下是主要代码: program Pac; {$R *.res} uses ExceptionLog, Forms, MainForm in 'Ma

我获得了一个关于如何在创建主窗体之前创建登录屏幕的示例。 然而,我不知道如何在登录屏幕关闭之前获取变量。我正在尝试传递变量

SelectedUserName : String;
SelectedUserIdNo, SelectedCoyId : Integer;
从LoginForm到mainform进行进一步处理

任何想法

提前谢谢

以下是主要代码:

program Pac;

{$R *.res}

uses
  ExceptionLog, Forms,
  MainForm in 'Main\MainForm.pas' {MainFormFrm} ,
  Datamodule in 'Main\Datamodule.pas' {DataModuleFrm: TDataModule} ,
  Login in 'Security\Login.pas' {LoginFrm};

begin
  if tLoginFrm.Execute then
  begin
    Application.Initialize;
    Application.MainFormOnTaskbar := True;
    Application.CreateForm(TMainFormFrm, MainFormFrm);
    Application.CreateForm(TDataModuleFrm, DataModuleFrm);
    Application.Run;
  end
  else
  begin
    Application.MessageBox
      ('You are not authorized to use the application. The password is "delphi".',
      'Password Protected Delphi application');
  end;
end.
我的登录码是:

unit Login;

interface

uses
  Windows, .. .. ..;

type
  TLoginFrm = class(TForm)
    Label1: TLabel;
    ButtOk: TButton;
    ButtCancel: TButton;
    cxMaskEditUserId: TcxMaskEdit;
    cxMaskEditPw: TcxMaskEdit;
    ButtReset: TButton;
    Label2: TLabel;
    QueryUser: TMSQuery;
    MSConnectionMain: TMSConnection;
    procedure ButtOkClick(Sender: TObject);
    procedure CheckMenuAccess;
    procedure ButtResetClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  public
    SelectedUserName: String;
    SelectedUserIdNo, SelectedCoyId: Integer;
    { Public declarations }
    class function Execute: boolean;
  end;

implementation

uses DataModule, MainForm, OutletListing;

{$R *.dfm}

class function TLoginFrm.Execute: boolean;
begin
  with TLoginFrm.Create(nil) do
    try
      Result := ShowModal = mrOk;
    finally
      Free;
    end;
end;

procedure TLoginFrm.FormShow(Sender: TObject);
begin
  MSConnectionMain.Connected := True;
end;

procedure TLoginFrm.ButtOkClick(Sender: TObject);
begin
  { Verify users are in list of users }
  With QueryUser Do
  Begin
    Active := False;
    if cxMaskEditUserId.EditValue = Null then
      ParamByName('UserId').Clear
    ELSE
      ParamByName('UserId').AsString := cxMaskEditUserId.EditValue;
    if cxMaskEditUserId.EditValue = Null then
      ParamByName('Userpassword').Clear
    ELSE
      ParamByName('Userpassword').AsString := cxMaskEditPw.EditValue;
    Active := True;
    If (FieldByName('UserId').IsNull) or
      (cxMaskEditUserId.EditValue = Null) Then
    Begin
      cxMaskEditUserId.EditValue := Null;
      cxMaskEditPw.EditValue := Null;
      cxMaskEditUserId.SetFocus;
    End
    Else
    Begin
      OutletListingFrm := TOutletListingFrm.Create(Self);
      SelectedUserIdNo := FieldByName('UserIdNo').AsInteger;
      SelectedUserName := FieldByName('UserName').AsString;
      OutletListingFrm.SelectedUserId := FieldByName('UserIdNo').AsInteger;
      IF OutletListingFrm.ShowModal = mrOk THEN
      BEGIN
        SelectedCoyId := FieldByName('CoyId').AsInteger;
        ModalResult := mrOk;
      END
      ELSE
        ModalResult := mrCancel;
      OutletListingFrm.Free;
    End;

 End;

 end.

创建包含从登录表单返回的信息的记录:

type
  TLoginInfo = record
    SelectedUserName: string;
    SelectedUserIdNo: Integer;
    SelectedCoyId: Integer;
  end;
然后从login类的
Execute
方法返回这样的记录:

function Execute(out LoginInfo: TLoginInfo): Boolean;
如果登录成功,则
Execute
方法的实现需要填写这些详细信息

然后将信息传递到主窗体。在调用
Application.CreateForm
时不能这样做。因此,您需要在
tmainform
上使用一个不同的方法,在创建主窗体后可以调用该方法。该方法将接收成功登录返回的
TLoginInfo
记录

因此,对于
tmainform
,您可以添加一个名为
InitialiseWithLoginInfo
的公共方法

procedure InitialiseWithLoginInfo(const LoginInfo: TLoginInfo);
那么您的.dpr文件将如下所示:

var
  LoginInfo: TLoginInfo;

begin
  if tLoginFrm.Execute(LoginInfo) then
  begin
    Application.Initialize;
    Application.MainFormOnTaskbar := True;
    Application.CreateForm(TMainFormFrm, MainFormFrm);
    MainFormFrm.InitialiseWithLoginInfo(LoginInfo);
    Application.CreateForm(TDataModuleFrm, DataModuleFrm);
    Application.Run;
  end
  else
  begin
    Application.MessageBox
      ('You are not authorized to use the application. The password is "delphi".',
      'Password Protected Delphi application');
  end;
end.

thks david,但是tLogininfo在编译期间是未声明的标识符。那么,您需要声明它!是 啊我放置类型TLoginInfo=record SelectedUserName:string;selectedUserDNO:整数;SelectedCoyId:整数;结束;在登录表单中,它需要进入一个单元的接口部分,其他使用它的单元都会使用它。这可能意味着在登录单元中。这是因为该代码无效,并且与我的答案中的代码不同。这是一个类型转换,而且是无效的。但是应答中的代码调用一个方法,将
LoginInfo
作为参数传递。试着理解答案,而不是盲目地使用它们。对你来说,重要的是学习,而不是仅仅把它当作一个黑匣子。另外,请您学习投票和接受答案。