Navigation: TextEd > Extension and Scripts > Dialogs >

TOpenDialog

 

 

 

TOpenDialog displays a file-selection dialog.

 

The TOpenDialog component displays a Windows dialog box for selecting and opening files. The dialog does not appear at runtime until it is activated by a call to the Execute method. When the user selects a file and clicks OK, the dialog closes and the selected file path is stored in the FileName property.


 


Properties and methods



 

constructor Create(TComponent AOwner);

Creates and initializes a file-selection dialog.

 

Ex.

TOpenDialog openDlg = TOpenDialog.Create(form);


 

property string DefaultExt;

Specifies a default file extension.


Ex.

openDlg.DefaultExt = ".txt";


 

property string FileName;

Indicates the name and directory path of the last file selected. 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.


 

property TStrings Files;

List of selected file names. Files is a string list that contains each selected file name with its full directory path.


 

property string Filter;

Determines the file masks (filters) available in the dialog.

 

Ex.

OpenDlg.Filter = "Text files (*.txt)|*.TXT|Pascal files (*.pas)|*.PAS";


 

property int FilterIndex;

Determines which filter is selected by default when the dialog opens. Set FilterIndex to 1 to choose the first file type in the list as the default, or set FilterIndex to 2 to choose the second file type as the default, and so forth.


 

function bool Execute;

Displays the dialog box. Execute returns true when the user makes a selection and clicks OK, and returns false when the user closes the dialog without making a selection.

 

Ex.

TOpenDialog openDlg = TOpenDialog.Create(form);

openDlg.DefaultExt = ".txt";

openDlg.Filter = "Text files|*.txt";

if (openDlg.Execute) {

   memo.Lines.LoadFromFile(openDlg.FileName);

}

openDlg.Free;


 

procedure Free;

Destroys an object and frees its associated memory, if necessary.


 

property string InitialDir;

InitialDir determines the default directory displayed in the file-selection dialog when it opens. For example, to point the dialog at the WINDOWS\SYSTEM directory, set the value of InitialDir to C:\WINDOWS\SYSTEM.


 

property string Title;

Specifies the text in the dialog's title bar.


 


Usage



 

TForm f;
TButton b;
TMemo memo;
 
/////////////////////////////////////
// Open a file dialog and open the
// selected file in the memo.
/////////////////////////////////////
void btnClick(TObject Sender) {
   TOpenDialog openDlg = TOpenDialog.Create(f);
   openDlg.DefaultExt = ".txt";
   openDlg.Filter = "Text files|*.txt";
   if (openDlg.Execute) {
      memo.Lines.LoadFromFile(openDlg.FileName);
   }
   openDlg.Free;
}
 
// Main procedure
{
   // Create a new window (form)
   f = new TForm(nil);
   f.Caption = "Open a text";
   f.Position = poScreenCenter;
   f.Width = 400;
   f.Height = 300;
   
   // Add a button to the window
   b = new TButton(f);
   b.Name = "btnOpen";
   b.Parent = f;
   b.SetBounds(10, 20, 75, 25);
   b.Anchors = akLeft+akTop;
   b.Caption = "Open...";
   b.OnClick = &btnClick;
 
   // Add a text edit control (memo) to the window
   memo = new TMemo(f);
   memo.Name = "memoLorem";
   memo.Parent = f;
   memo.ScrollBars = ssBoth;
   memo.SetBounds(10, 50, 380, 240);
   memo.Anchors = akLeft+akTop+akRight+akBottom;
   memo.Lines.Text = "Lorem Ipsum iaculis audire mi moderatius\r\ncorpora dictumst turpis."; 
   
   // Show the window
   f.ShowModal;
   f.Free;
}


 


 


 

 

 

 

 

Copyright © 2022 Rickard Johansson