RAD Studio VCL Reference
|
Indicates the name and directory path of the last file selected.
property FileName: TFileName;
__property TFileName FileName;
The FileName property returns the name and complete directory path of the most recently selected file. The value of FileName is the same as the first item in the Files property.
To make a file name appear by default in the dialog's edit box, assign a value to FileName in the Object Inspector or in program code. Programmatic changes to FileName have no effect while the dialog is active.
if OpenDialog1.Execute then Memo1.Lines.LoadFromFile(OpenDialog1.FileName) else Memo1.Lines.Clear;
if (OpenDialog1->Execute()) Memo1->Lines->LoadFromFile(OpenDialog1->FileName); else Memo1->Clear();
C++ Examples:
/* This code displays a Save Picture dialog box with the TBitmap default extension that is added automatically to files that are given no extension. Confirm that the bitmap file has been saved under the filename and path specified. */ #include <memory> //for STL auto_ptr class void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString NewFileName, OldFileName; SavePictureDialog1->DefaultExt = GraphicExtension(__classid(Graphics::TBitmap)); SavePictureDialog1->Filter = GraphicFilter(__classid(Graphics::TBitmap)); OldFileName = "factory.bmp"; if (SavePictureDialog1->Execute()) { NewFileName = SavePictureDialog1->FileName; std::auto_ptr<TFileStream> OldFile(new TFileStream(OldFileName, (fmOpenRead | fmShareDenyWrite))); std::auto_ptr<TFileStream> NewFile(new TFileStream(NewFileName, (fmCreate | fmShareDenyRead))); NewFile->CopyFrom(OldFile.get(), OldFile->Size); }; } void __fastcall TForm1::FormCreate(TObject *Sender) { Image1->Picture->LoadFromFile("../factory.bmp"); }
/* This example uses a tab control to display the contents of several files. To run the example, place a tab control on a form and add a memo control that fits into its client area. Be sure to leave enough room for the tabs when they appear. Then add an OpenDialog and a button to the form. The "Add a file" button adds a single file to the tabcontrol. The "Assign files" button removes previous files and can be used to assign multiple files. To assign multiple files, use CNTL Select or SHIFT Select to select files in the OpenDialog. */ void __fastcall TForm1::Add_a_fileClick(TObject *Sender) { OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly; if (OpenDialog1->Execute()) { int index = TabControl1->Tabs->Add(OpenDialog1->FileName); Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[index]); TabControl1Change(Sender); } } void __fastcall TForm1::Assign_filesClick(TObject *Sender) { OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly; if (OpenDialog1->Execute()) { TabControl1->Tabs->Assign(OpenDialog1->Files); Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[TabControl1->TabIndex]); } } /* Place the following code in the tab control’s OnChange event handler: */ void __fastcall TForm1::TabControl1Change(TObject *Sender) { Memo1->Lines->LoadFromFile( TabControl1->Tabs->Strings[TabControl1->TabIndex]); }
/* The following code allows the user to use a dialog box to redefine the icon for the application at run time. When the user clicks Button1, OpenDialog1 executes and the user specifies an icon filename. The Icon is then assigned to the application. At runtime, click Button1 to select the icon, then minimize the application to see the icon. */ void __fastcall TForm1::Button1Click(TObject *Sender) { OpenPictureDialog1->DefaultExt = GraphicExtension(__classid(TIcon)); OpenPictureDialog1->FileName = GraphicFileMask(__classid(TIcon)); OpenPictureDialog1->Filter = GraphicFilter(__classid(TIcon)); OpenPictureDialog1->Options.Clear(); OpenPictureDialog1->Options << ofFileMustExist << ofHideReadOnly << ofNoChangeDir; while (true) { if (OpenPictureDialog1->Execute()) { if (!OpenPictureDialog1->Options.Contains(ofExtensionDifferent)) { Application->Icon->LoadFromFile(OpenPictureDialog1->FileName); break; } else // reset Options to remove ofExtensionDifferent { OpenPictureDialog1->Options.Clear(); OpenPictureDialog1->Options << ofFileMustExist << ofHideReadOnly << ofNoChangeDir; } } else // user cancelled break; } }
/* Click the button to open a TOpenDialog, then select a file to delete. */ void __fastcall TForm1::Button1Click(TObject *Sender) { OpenDialog1->Title = "Delete File"; if (OpenDialog1->Execute()) { if (FileExists(OpenDialog1->FileName)) DeleteFile(OpenDialog1->FileName); } }
Delphi Examples:
{ This code displays a Save Picture dialog box with the TBitmap default extension that is added automatically to files that are given no extension. Confirm that the bitmap file has been saved under the filename and path specified. } procedure TForm1.Button1Click(Sender: TObject); var NewFileName, OldFileName: string; NewFile, OldFile: TFileStream; begin SavePictureDialog1.DefaultExt := GraphicExtension(TBitmap); SavePictureDialog1.Filter := GraphicFilter(TBitmap); OldFileName := 'factory.bmp'; if SavePictureDialog1.Execute then begin NewFileName := SavePictureDialog1.FileName; OldFile := TFileStream.Create( OldFileName, fmOpenRead or fmShareDenyWrite); try NewFile := TFileStream.Create( NewFileName, fmCreate or fmShareDenyRead); try NewFile.CopyFrom(OldFile, OldFile.Size); finally FreeAndNil(NewFile); end; finally FreeAndNil(OldFile); end; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Image1.Picture.LoadFromFile('factory.bmp'); end;
{ This example uses a tab control to display the contents of several files. To run the example, place a tab control on a form and add a memo control that fits into its client area. Be sure to leave enough room for the tabs when they appear. Then add an OpenDialog and a button to the form. The "Add a file" button adds a single file to the tabcontrol. The "Assign files" button removes previous files and can be used to assign multiple files. To assign multiple files, use CNTL Select or SHIFT Select to select files in the OpenDialog. } procedure TForm1.Add_a_fileClick(Sender: TObject); var index : integer; begin OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly]; if OpenDialog1.Execute then begin index:= TabControl1.Tabs.Add(OpenDialog1.FileName); Memo1.Lines.LoadFromFile(TabControl1.Tabs[index]); TabControl1Change(Sender); end; end; procedure TForm1.Assign_filesClick(Sender: TObject); var index : integer; begin OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly]; if OpenDialog1.Execute then begin TabControl1.Tabs.Assign(OpenDialog1.Files); Memo1.Lines.LoadFromFile( TabControl1.Tabs[TabControl1.TabIndex]); end; end; { Place the following code in the tab control’s OnChange event handler: } procedure TForm1.TabControl1Change(Sender: TObject); begin with TabControl1 do Memo1.Lines.LoadFromFile(Tabs[TabIndex]); end;
{ The following code allows the user to use a dialog box to redefine the icon for the application at run time. When the user clicks Button1, OpenDialog1 executes and the user specifies an icon filename. The Icon is then assigned to the application. At runtime, click Button1 to select the icon, then minimize the application to see the icon. } procedure TForm1.Button1Click(Sender: TObject); var Done: Boolean; filenamestring : String; begin OpenPictureDialog1.DefaultExt := GraphicExtension(TIcon); filenamestring := GraphicFileMask(TIcon); OpenPictureDialog1.FileName := filenamestring; OpenPictureDialog1.Filter := GraphicFilter(TIcon); OpenPictureDialog1.Options := [ ofFileMustExist, ofHideReadOnly, ofNoChangeDir ]; Done := False; while not Done do begin if OpenPictureDialog1.Execute then begin if not (ofExtensionDifferent in OpenPictureDialog1.Options) then begin Application.Icon.LoadFromFile(OpenPictureDialog1.FileName); Done := True; end else // OpenPictureDialog1.Options := OpenPictureDialog1.Options - ofExtensionDifferent; end else { User cancelled } Done := True; end; end;
{ Use a TOpenDialog to select a file. Place the first line of the file into a TEdit. Click on the text edit to open the open dialog box. } procedure TForm1.Edit1Click(Sender: TObject); var F: TextFile; S: string; begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(F, OpenDialog1.FileName); { File selected in dialog } Reset(F); Readln(F, S); { Read first line of file } Edit1.Text := S; { Put string in a TEdit control } CloseFile(F); end; end;
{ Click the button to open a TOpenDialog, then select a file to delete. } procedure TForm1.Button1Click(Sender: TObject); var F: Textfile; begin OpenDialog1.Title := 'Delete File'; if OpenDialog1.Execute then begin AssignFile(F, OpenDialog1.FileName); try Reset(F); if MessageDlg('Erase ' + OpenDialog1.FileName + '?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin CloseFile(F); Erase(F); end; except on EInOutError do MessageDlg('File I/O error.', mtError, [mbOk], 0); end; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|