对窗体属性的赋值 ( 积分: 10 )

  • 主题发起人 主题发起人 computerzzz
  • 开始时间 开始时间
C

computerzzz

Unregistered / Unconfirmed
GUEST, unregistred user!
我在登陆对话框中对主对话框的属性赋值,但是在主对话框中的属性还是为空.

program Project1;

uses
Forms,
MainFrm in 'MainFrm.pas' {MainForm},
LoginFrm in 'LoginFrm.pas' {Login},
dmunit in 'dmunit.pas' {DM: TDataModule};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TDM, DM);
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TLogin,Login);
Login.ShowModal;
Login.Free;
Application.Run;
end.
unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, TFlatButtonUnit, Menus;

type
TMainForm = class(TForm)
FlatButton1: TFlatButton;
FlatButton2: TFlatButton;
FlatButton3: TFlatButton;
FlatButton4: TFlatButton;
FlatButton5: TFlatButton;
FlatButton6: TFlatButton;
StatusBar1: TStatusBar;
FlatButton7: TFlatButton;
FlatButton8: TFlatButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
user:string;
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}



procedure TMainForm.FormCreate(Sender: TObject);
var
ss:string;
begin
ss:=MainForm.user;
MainForm.StatusBar1.Panels[0].Text:='日期:'+DateToStr(Date);
MainForm.StatusBar1.Panels[1].Width:=100;
MainForm.StatusBar1.Panels[1].Text:=
'操作员:'+MainForm.user;{己经对MainForm.user赋过值了并不为空!!}

end;

end.
unit LoginFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB;

type
TLogin = class(TForm)
Label1: TLabel;
Label2: TLabel;
Name: TEdit;
PWD: TEdit;
Image1: TImage;
Ok: TBitBtn;
Cancel: TBitBtn;
procedure OkClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
times:integer;
end;

var
Login: TLogin;
implementation

{$R *.dfm}
uses dmunit,MainFrm;
procedure TLogin.OkClick(Sender: TObject);
var
count:integer;
begin
if times<3 then
begin
inc(times);
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select * from ausers');
dm.ADOQuery1.SQL.Add('where User_no=:szName and Password=:pass');
dm.ADOQuery1.Parameters.ParamByName('szName').Value:=Trim(Name.Text);
dm.ADOQuery1.Parameters.ParamByName('pass').Value:=PWD.Text;
dm.ADOQuery1.Open;
count:=dm.ADOQuery1.RecordCount;
if count=0 then
begin
MessageBox(self.Handle,'口令不正确',' 提示信息',MB_ICONINFORMATION);
end
else
begin
MessageBox(self.Handle,'OK','INFOR',mb_iconinformation);
MainForm.user:=Name.Text;{对MainFrom属性赋值}
MainForm.ShowModal;
dm.ADOQuery1.Close;
{MainForm.Free;}
end;{if}
end
else
begin
MessageBox(self.Handle,'输入次数己超过3次','提示信息',MB_ICONINFORMATION);
Application.Terminate;
end;{if}
dm.ADOQuery1.Close;
end;
procedure TLogin.FormCreate(Sender: TObject);
begin
times:=0;
end;

procedure TLogin.CancelClick(Sender: TObject);
begin
Application.Terminate;
end;

end.
 
我在登陆对话框中对主对话框的属性赋值,但是在主对话框中的属性还是为空.

program Project1;

uses
Forms,
MainFrm in 'MainFrm.pas' {MainForm},
LoginFrm in 'LoginFrm.pas' {Login},
dmunit in 'dmunit.pas' {DM: TDataModule};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TDM, DM);
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TLogin,Login);
Login.ShowModal;
Login.Free;
Application.Run;
end.
unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, TFlatButtonUnit, Menus;

type
TMainForm = class(TForm)
FlatButton1: TFlatButton;
FlatButton2: TFlatButton;
FlatButton3: TFlatButton;
FlatButton4: TFlatButton;
FlatButton5: TFlatButton;
FlatButton6: TFlatButton;
StatusBar1: TStatusBar;
FlatButton7: TFlatButton;
FlatButton8: TFlatButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
user:string;
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}



procedure TMainForm.FormCreate(Sender: TObject);
var
ss:string;
begin
ss:=MainForm.user;
MainForm.StatusBar1.Panels[0].Text:='日期:'+DateToStr(Date);
MainForm.StatusBar1.Panels[1].Width:=100;
MainForm.StatusBar1.Panels[1].Text:=
'操作员:'+MainForm.user;{己经对MainForm.user赋过值了并不为空!!}

end;

end.
unit LoginFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB;

type
TLogin = class(TForm)
Label1: TLabel;
Label2: TLabel;
Name: TEdit;
PWD: TEdit;
Image1: TImage;
Ok: TBitBtn;
Cancel: TBitBtn;
procedure OkClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
times:integer;
end;

var
Login: TLogin;
implementation

{$R *.dfm}
uses dmunit,MainFrm;
procedure TLogin.OkClick(Sender: TObject);
var
count:integer;
begin
if times<3 then
begin
inc(times);
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select * from ausers');
dm.ADOQuery1.SQL.Add('where User_no=:szName and Password=:pass');
dm.ADOQuery1.Parameters.ParamByName('szName').Value:=Trim(Name.Text);
dm.ADOQuery1.Parameters.ParamByName('pass').Value:=PWD.Text;
dm.ADOQuery1.Open;
count:=dm.ADOQuery1.RecordCount;
if count=0 then
begin
MessageBox(self.Handle,'口令不正确',' 提示信息',MB_ICONINFORMATION);
end
else
begin
MessageBox(self.Handle,'OK','INFOR',mb_iconinformation);
MainForm.user:=Name.Text;{对MainFrom属性赋值}
MainForm.ShowModal;
dm.ADOQuery1.Close;
{MainForm.Free;}
end;{if}
end
else
begin
MessageBox(self.Handle,'输入次数己超过3次','提示信息',MB_ICONINFORMATION);
Application.Terminate;
end;{if}
dm.ADOQuery1.Close;
end;
procedure TLogin.FormCreate(Sender: TObject);
begin
times:=0;
end;

procedure TLogin.CancelClick(Sender: TObject);
begin
Application.Terminate;
end;

end.
 
刚下你的文件,有空时帮你搞定
 
问题出在对DELPHI的事件不理解造成的误解,

Application.CreateForm(TMainForm, MainForm);//创建窗体时就触发了OnCreate事件,该事件调用FormCreate,时间在执行TLogin创建/显示/执行之前

procedure TMainForm.FormCreate(Sender: TObject);
var
ss:string;
begin
ss:=MainForm.user;//进入本方法时并未对User赋值,创建主窗体时TLogin尚未创建.
MainForm.StatusBar1.Panels[0].Text:='日期:'+DateToStr(Date);
MainForm.StatusBar1.Panels[1].Width:=100;
MainForm.StatusBar1.Panels[1].Text:=
'操作员:'+MainForm.user;{己经对MainForm.user赋过值了并不为空!!}
end;

正确的作法是在将MainForm.StatusBar1.Panels[1].Text:=
'操作员:'+MainForm.user;放到OnActive事件中.
 
to xrs
解决了,谢谢了!
不知能否留个e-mail,本人刚学DELPHI不久,望能得高手指点一二 。
可以的话,给我的邮箱发一封信!!!!
 

Similar threads

I
回复
0
查看
473
import
I
S
回复
0
查看
842
SUNSTONE的Delphi笔记
S
I
回复
0
查看
745
import
I
I
回复
0
查看
597
import
I
后退
顶部