一个对话框返回字符串的简单问题!(50分)

  • 主题发起人 主题发起人 icola
  • 开始时间 开始时间
I

icola

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟刚入门,这个问题感觉不难,但已经耗费了一上午时间了,家产不多,请不要嫌分少。

我在主窗体frmMail中调用对话框,运行总出错,不知怎么回事。
//主窗体调用代码
procedure TfrmMain.Open1Click(Sender: TObject);
var
OKRightDlg:TOKRightDlg;
MDBDir,DBName:string;
begin
MDBDir:='D/*.mdb';
OKRightDlg:=TOKRightDlg.Create(Self);
try
OKRightDlg.GetAllMDB(MDBDir)

OKRightDlg.ShowModal;
DBName:=OKRightDlg.GetMDBName;
ShowMessage(DBName);
finally
OKRightDlg.Free;
end;
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;
private
{ Private declarations }
public
procedure GetAllMDB(MDBDir:string);{ Public declarations }
function GetMDBName:string;
end;

var
OKRightDlg: TOKRightDlg;

implementation

{$R *.dfm}
{ TOKRightDlg }
uses
uMain;
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
OKBtn.ModalResult:=mrOK;
CancelBtn.ModalResult:=mrCancel;
if OKRightDlg.ShowModal=mrOK then
Result:=ListBox1.Items[ListBox1.ItemIndex];
if OKRightDlg.ShowModal=mrCancel then
Result:='';
end;


end.
 
>>MDBDir:='D/*.mdb';
这个 'D/*.mdb' 好像有问题哟... 改成 'D:/*.mdb' 再试试?
 
//主窗体调用代码
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.
 
To luzhouman:
function ShowOKRightDlg(const MDBDir: string
var DBName: string): Boolean;的实现部分应该有返回类型吧!
另外,我看到许多API函数的返回类型也为布尔型,你这里定义的ShowOKRightDlg函数也是布尔型,你觉得定义一个返回布尔类型的函数有什么好处呢?谢谢,我是新手。
 
对不起,少写了一点代码, 改好了
在这里返回Boolean是检查是否按下了OKBtn, 如果按下了就取出ListBox1选择的Item
 
接受答案了.
 
后退
顶部