关于对话框返回字符串的问题!(50分)

  • 主题发起人 主题发起人 lining1980
  • 开始时间 开始时间
L

lining1980

Unregistered / Unconfirmed
GUEST, unregistred user!
我自定义了一个对话框,无论输入什么,返回字符串总为空,感觉应该没问题,真奇怪!
/////////////////
自定义对话框代码
////////////////
unit uInputDialog;

interface

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

type
TfrmNewProject = class(TForm)
edtNewName: TEdit;
GroupBox1: TGroupBox;
bbtnOK: TBitBtn;
bbtnCancel: TBitBtn;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
function InputDialog(DefaultName,NewName:string):Boolean;
end;

var
frmNewProject: TfrmNewProject;

implementation

{$R *.dfm}

procedure TfrmNewProject.FormCreate(Sender: TObject);
begin
bbtnOK.ModalResult:=mrOK;
bbtnCancel.ModalResult:=mrCancel;
end;

function TfrmNewProject.InputDialog(DefaultName,NewName: string): Boolean;
var
frmNewProject:TfrmNewProject;
begin
NewName:=DefaultName;
Result:=False;
frmNewProject:=TfrmNewProject.Create(nil);
edtNewName.Text:=DefaultName;
try
if frmNewProject.ShowModal=mrOk then
begin
NewName:=edtNewName.Text;
Result:=True;
end;
finally
frmNewProject.Free;
end;
end;

end.
///////////////////
主程序调用
////////////////////
procedure TfrmMain.btn1Click(Sender: TObject);
var
AProjectName:string;
begin
if frmNewProject.InputDialog('新工程',AProjectName) then
ShowMessage(AProjectName);
 
应该把newname返回才行吧,你的AProjectName不是返回值!
 
To szmh:
那你如何解释下面的ShowOKRightDlg函数,DBName虽然不是返回值,不是也可以当作结果显示出来吗?
procedure TfrmMain.Open1Click(Sender: TObject);
var
DBName: string;
begin
if ShowOKRightDlg('D:/*.mdb', DBName) then ShowMessage(DBName);
end;


//对话框代码如下
unit uOpenPrtDialog;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
Buttons, ExtCtrls, ComCtrls;

type
TOKRightDlg = class(TForm)
OKBtn: TButton;
CancelBtn: TButton;
GroupBox1: TGroupBox;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
public
procedure GetAllMDB(MDBDir:string);
function GetMDBName:string;
end;

function ShowOKRightDlg(const MDBDir: string
var DBName: string): Boolean;

var
OKRightDlg: TOKRightDlg;

implementation

{$R *.dfm}

function ShowOKRightDlg(const MDBDir: string
var DBName: string): Boolean;
begin
DBName:='';
Result:=False

OKRightDlg:=TOKRightDlg.Create(nil);
try
OKRightDlg.GetAllMDB(MDBDir);
if OKRightDlg.ShowModal=MrOK then
begin
DBName:=OKRightDlg.GetMDBName;
Result:=True;
end;
finally
OKRightDlg.Free;
end;
end;

procedure TOKRightDlg.GetAllMDB(MDBDir: string);
var
SearchRec:TSearchRec;
Path:string;
Attr,Found:integer;
begin
Path:=MDBDir;
Attr:=faAnyFile;
Found:=FindFirst(Path,Attr,SearchRec);
while Found=0 do
begin
ListBox1.Items.Add(SearchRec.Name);
Found:=FindNext(SearchRec);
end;
FindClose(SearchRec);
end;

function TOKRightDlg.GetMDBName:string;
begin
if ListBox1.ItemIndex>=0 then
Result:=ListBox1.Items[ListBox1.ItemIndex];
end;

procedure TOKRightDlg.FormCreate(Sender: TObject);

begin
OKBtn.ModalResult:=mrOK;
CancelBtn.ModalResult:=mrCancel;
end;

end.
 
小同学,参数要返回,
必须前面加var,
好好复习:过程参数,第一单元,
马上考试了,
少上网,别不及格
 
谢谢二位大虾指导,我只是个Delphi爱好者,刚学不久,没有系统学过理论,都是从实践入手。不过我在InputDialog方法的NewName参数前加上var后,仍然有问题:无论在edit中输入什么字符串,Application.Message的对话框中都返回“工程“新工程”建立成功!”,而不是随着edit中的内容变化,代码如下:
///////////////////
自定义对话框
///////////////////
unit uInputDialog;

interface

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

type
TfrmNewProject = class(TForm)
edtNewName: TEdit;
GroupBox1: TGroupBox;
bbtnOK: TBitBtn;
bbtnCancel: TBitBtn;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
function InputDialog(const DefaultName:string;var NewName:string):Boolean;{ Public declarations }
end;

var
frmNewProject: TfrmNewProject;

implementation

{$R *.dfm}

procedure TfrmNewProject.FormCreate(Sender: TObject);
begin
bbtnOK.ModalResult:=mrOK;
bbtnCancel.ModalResult:=mrCancel;
end;

function TfrmNewProject.InputDialog(const DefaultName:string;var NewName: string): Boolean;
var
frmNewProject:TfrmNewProject;
begin
NewName:=DefaultName;
Result:=False;
frmNewProject:=TfrmNewProject.Create(nil);
edtNewName.Text:=DefaultName;
try
if frmNewProject.ShowModal=mrOk then
begin
NewName:=edtNewName.Text;
Result:=True;
end;
finally
frmNewProject.Free;
end;
end;

end.
//////////////
主程序调用代码
//////////////
begin
if frmNewProject.InputDialog('新工程',AProjectName) then
begin
Screen.Cursor:=crHourGlass;
if CopyFile(PChar(EXEPath+'ProjectDB/ModelDB/ModelDB.mdb'),PChar(Format(EXEPath+'ProjectDB/%s.mdb',[AProjectName])),False) then
begin
Screen.Cursor:=crDefault;
Application.MessageBox(PChar(Format('工程“%s”建立成功!',[AProjectName])),'新建工程',MB_OK+MB_IconInformation);
end;
end;
end;
 
function TfrmNewProject.InputDialog(const DefaultName:string;var NewName: string): Boolean;
begin
NewName:=DefaultName;
Result:=False;
frmNewProject:=TfrmNewProject.Create(nil);
frmNewProject.edtNewName.Text:=DefaultName;
try
if frmNewProject.ShowModal=mrOk then
begin
NewName:=frmNewProject.edtNewName.Text;
Result:=NewName<>'';
end;
finally
frmNewProject.Free;
end;
end;
 
谢谢,看来我犯的都是低级错误,需要好好学习一下基础知识。散分了,分不多,请见量!
 
后退
顶部