Breaking News
Loading...
Sunday, August 4, 2013

The TOpenDialog is a visual component. It is used to allow a user to select one or more files to open.

It can be defined by dragging the open dialog icon from the Dialogs tab in Delphi, or by defining a TOpenDialog variable.


Creating the dialog object:



var
openDialog : TOpenDialog;
begin
openDialog := TOpenDialog.Create(self);

Note that the dialog must have an anchor – here we provide the current object – self – as the anchor.Simple Sample Open Dialog 1 300x277


Setting Filter:

[/sociallocker]



//For one extension files
openDialog.Filter := 'Text files only|*.txt';
//For two or more extension files
openDialog.Filter := 'Text and Word files only|*.txt;*.doc';

Here’s the full source code:



//******************************************//
// Sampel Source Code Learning Delphi //
// OpenDialog //
// by : G. Giarto //
// E-Mail : support@learning-delphi.com //
// Website: http://www.learning-delphi.com //
//******************************************//

unit Unit1;

interface

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

type
TForm1 = class(TForm)
btn1: TButton;
dlgOpen1: TOpenDialog;
lbl1: TLabel;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
openDialog : TOpenDialog; // Open dialog variable
begin
// Create the open dialog object - assign to our open dialog variable
openDialog := TOpenDialog.Create(self);

// Set up the starting directory to be the current one
openDialog.InitialDir := GetCurrentDir;

// Only allow existing files to be selected
openDialog.Options := [ofFileMustExist];

// Allow only .txt to be selected
openDialog.Filter :=
'Text files|*.txt';

// Select pascal files as the starting filter type
openDialog.FilterIndex := 1;

// Display the open file dialog
if openDialog.Execute
then
begin
lbl1.Caption := openDialog.FileName;
mmo1.Lines.LoadFromFile(openDialog.FileName);
end
else ShowMessage('Open file was cancelled');

// Free up the dialog
openDialog.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := 'Open Dialog [Simple Sample]';
mmo1.Clear;
lbl1.Caption := '';
end;
end.

After the run will appear as follows:


Simple Sample Open Dialog 2 300x262


Simple Sample Open Dialog 3 300x276

0 comments:

Post a Comment

 

Receive All Free Updates Via Facebook.