1

我尝试在 Delphi 中制作文件管理器,我需要能够创建新文件夹。

所以,我得到了我的主表单,当我按下按钮时Create New Folder,会出现其他表单,我可以在其中输入新文件夹名称并确认或取消创建。

所以我为文件夹创建创建了新表单并使其不可见。

我是这样做的-在这里我在主窗体中得到了程序

procedure TfolderFrame.CreateFolder;
begin
  newFolderDialog.Visible:=true;
end;

这是新的文件夹形式

unit FolderDialog;

interface

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

type
  TnewFolderDialog = class(TForm)
    edtName: TEdit;
    lblName: TLabel;
    btnOK: TButton;
    btnCancel: TButton;
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    FolderName:String;
    kindOfAction:char;
    hasUpdated:Boolean;
  end;

var
  newFolderDialog: TnewFolderDialog;

implementation

{$R *.dfm}

procedure TnewFolderDialog.btnOKClick(Sender: TObject);
begin
  FolderName:=edtName.Text;
  if CreateDir(FolderName)
  then begin
    ShowMessage('New folder created!');

  end
  else begin
    ShowMessage('Creation failed. Error : '+ IntToStr(GetLastError));
  end;
  newFolderDialog.edtName.Clear;
  newFolderDialog.Close;
  hasUpdated:=True;
end;

procedure TnewFolderDialog.btnCancelClick(Sender: TObject);
begin
  newFolderDialog.edtName.Clear;
  newFolderDialog.Close;
end;

procedure TnewFolderDialog.FormActivate(Sender: TObject);
begin
  hasUpdated:=false;
end;

end.

问题是 -TfolderFrame.CreateFolder;调用它时只会使新文件夹形式可见,然后过程结束。但是我需要在创建文件夹后做一些其他的事情,比如Refresh或东西。

我一直在尝试这样做:

 procedure TfolderFrame.CreateFolder;
    begin
      newFolderDialog.Visible:=true;
     while not (newFolderDialog.hasUpdated) do begin
       if(newFolderDialog.hasUpdated) then
       RefreshAllStuff;
     end;
    end;

但程序只是因为它而卡住了。

只有在Form2中确认文件夹创建后,如何才能在Form1Refresh中调用过程?

4

1 回答 1

2

重新设计您的代码以TForm.ShowModal()改用,例如:

procedure TfolderFrame.CreateFolder;
begin
  if newFolderDialog.ShowModal = mrOk then
    RefreshAllStuff;
end;

procedure TnewFolderDialog.btnOKClick(Sender: TObject);
begin
  FolderName := edtName.Text;
  if CreateDir(FolderName) then
  begin
    ShowMessage('New folder created!');
    ModalResult := mrOk;
  end
  else
    ShowMessage('Creation failed. Error : '+ IntToStr(GetLastError));
end;

procedure TnewFolderDialog.btnCancelClick(Sender: TObject);
begin
  ModalResult =: mrCancel;
end;

procedure TnewFolderDialog.FormShow(Sender: TObject);
begin
  edtName.Clear;
end;
于 2013-09-29T18:11:14.337 回答