PDA

View Full Version : Tutorial to make a Browse Button (TOpenDialog)



Ron
04-13-2007, 07:43 PM
Tutorial to make a Browse Button (TOpenDialog)
by Ron

Screenshot:
http://img76.imageshack.us/img76/4448/browseformlk9.png

First start off with a new program in SCAR Divi. You should have this.


program New;
begin
end.


Add these variables after "program New;". You will be creating a form with an edit box, a browse button, a memo box, and of course, the browse window (TOpenDialog).


var
frmDesign : TForm;
Edit1 : TEdit;
Button1 : TButton;
OpenDialog1 : TOpenDialog;
Memo1 : TMemo;


Add the Browse procedure code right after the variable code.


// When you click on the Browse button, this code will be executed.
procedure Browse(Sender : TObject);
begin
// First create the OpenDialog.
OpenDialog1 := TOpenDialog.Create(frmDesign);
// Set its opening directory to the AppPath or the path to where
// scar is located on your computer.
OpenDialog1.InitialDir := AppPath;
// Make sure in the options that the file exists and opens the file
// as ReadOnly which means your program can't write to it.
OpenDialog1.Options := [ofFileMustExist, ofReadOnly];
// Set up all the different files you want the Filter to look for.
// "Text Documents (*.txt)" is the name and "|*.txt|" is the file the
// dialog displays. The second option is All Files which shows all
// the files. Notice the single asterisk |*| for All Files.
OpenDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
// If the use selected a file, then display the filename and set
// Edit1.Text to the filename.
if(OpenDialog1.Execute)then
begin
WriteLn('User selected this file: ' + OpenDialog1.FileName);
Edit1.Text := OpenDialog1.FileName;
LoadFromFile(Memo1, OpenDialog1.FileName);
end else // Otherwise show that the User pressed cancel.
WriteLn('User pressed cancel!')
OpenDialog1.Free; // Finally, free the OpenDialog1 from memory so
// your computer will run more efficiently. :)
end;


Now add the form procedures right after the browse button code.


// The main form's code.
procedure InitForm;
begin
frmDesign := CreateForm; // Create the form.
frmDesign.Caption := 'Browse (OpenDialog) by Ron';
frmDesign.Position := poScreenCenter; // Center the form on your screen.
frmDesign.Borderstyle := bsSingle; // Make the form not resizeable.
frmDesign.Bordericons := [biMinimize, biSystemMenu]; // Remove maximize button.
frmDesign.ClientHeight := 260;
frmDesign.ClientWidth := 314;
Edit1 := TEdit.Create(frmDesign); // Create the Edit box.
Edit1.Parent := frmDesign;
Edit1.Top := 15;
Edit1.Left := 15;
Edit1.Width := 200;
Button1 := TButton.Create(frmDesign); // Create the Browse Button.
Button1.Parent := frmDesign;
Button1.Top := 13;
Button1.Left := 226;
Button1.Caption := 'Browse';
Memo1 := TMemo.Create(frmDesign); // Create the big text box.
Memo1.Parent := frmDesign;
Memo1.Top := 50;
Memo1.Left := 15;
Memo1.Height := 200;
Memo1.Width := 284;
Memo1.Scrollbars := ssBoth; // Make sure the box has both scrollbars.
end;

// Safely call the InitForm.
procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

// Make the form show itself.
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

// Safely show the form's modal.
procedure SafeShowFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowFormModal', V);
end;

// Here is the MainInitForm used to run the procedures correctly.
procedure MainInitForm;
begin
// Here is a try/finally/except way of checking for errors. This
// will try to show the form. If there is an error, it will run the
// except code. The finally code will always be executed.
try
// Show the form.
SafeInitForm;
SafeShowFormModal;
finally
// Free the form from memory to avoid memory leaks.
FreeForm(frmDesign);
except
// Print an error message if by unfortunate chance that the form
// experiences an error.
WriteLn('An error seems to have occurred in: InitForm');
end;
end;


Add this code right after "Button1.Caption := 'Browse';" to make the Browse button run the procedure Browse;


Button1.OnClick := @Browse; // When the button is clicked, run the procedure Browse;


And finally make this your main begin/end. code at the bottom of your script.


begin
ClearDebug; // Clear the debug box to make SCAR nice and clean.
MainInitForm; // Run the MainInitForm! :)
end.


Here is all the code with Line by line explanation with comments.


{------------------------------------------------------------------]
Title: Browse Button (TOpenDialog) Example by Ron
Author: Ron
SCAR Version: SCAR Divi 3.0
Copyright: April 13, 2007

Instructions: Just run the script, click browse, select a file
and see what happens! Read the comments to see how this works!

Love you,
Ron
[------------------------------------------------------------------}


program BrowseButtonExampleByRon;

var
frmDesign : TForm;
Edit1 : TEdit;
Button1 : TButton;
OpenDialog1 : TOpenDialog;
Memo1 : TMemo;

// When you click on the Browse button, this code will be executed.
procedure Browse(Sender : TObject);
begin
// First create the OpenDialog.
OpenDialog1 := TOpenDialog.Create(frmDesign);
// Set its opening directory to the AppPath or the path to where
// scar is located on your computer.
OpenDialog1.InitialDir := AppPath;
// Make sure in the options that the file exists and opens the file
// as ReadOnly which means your program can't write to it.
OpenDialog1.Options := [ofFileMustExist, ofReadOnly];
// Set up all the different files you want the Filter to look for.
// "Text Documents (*.txt)" is the name and "|*.txt|" is the file the
// dialog displays. The second option is All Files which shows all
// the files. Notice the single asterisk |*| for All Files.
OpenDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
// If the use selected a file, then display the filename and set
// Edit1.Text to the filename.
if(OpenDialog1.Execute)then
begin
WriteLn('User selected this file: ' + OpenDialog1.FileName);
Edit1.Text := OpenDialog1.FileName;
LoadFromFile(Memo1, OpenDialog1.FileName);
end else // Otherwise show that the User pressed cancel.
WriteLn('User pressed cancel!')
OpenDialog1.Free; // Finally, free the OpenDialog1 from memory so
// your computer will run more efficiently. :)
end;

// The main form's code.
procedure InitForm;
begin
frmDesign := CreateForm; // Create the form.
frmDesign.Caption := 'Browse (OpenDialog) by Ron';
frmDesign.Position := poScreenCenter; // Center the form on your screen.
frmDesign.Borderstyle := bsSingle; // Make the form not resizeable.
frmDesign.Bordericons := [biMinimize, biSystemMenu]; // Remove maximize button.
frmDesign.ClientHeight := 260;
frmDesign.ClientWidth := 314;
Edit1 := TEdit.Create(frmDesign); // Create the Edit box.
Edit1.Parent := frmDesign;
Edit1.Top := 15;
Edit1.Left := 15;
Edit1.Width := 200;
Button1 := TButton.Create(frmDesign); // Create the Browse Button.
Button1.Parent := frmDesign;
Button1.Top := 13;
Button1.Left := 226;
Button1.Caption := 'Browse';
Button1.OnClick := @Browse; // When the button is clicked, run the procedure Browse;
Memo1 := TMemo.Create(frmDesign); // Create the big text box.
Memo1.Parent := frmDesign;
Memo1.Top := 50;
Memo1.Left := 15;
Memo1.Height := 200;
Memo1.Width := 284;
Memo1.Scrollbars := ssBoth; // Make sure the box has both scrollbars.
end;

// Safely call the InitForm.
procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

// Make the form show itself.
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

// Safely show the form's modal.
procedure SafeShowFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowFormModal', V);
end;

// Here is the MainInitForm used to run the procedures correctly.
procedure MainInitForm;
begin
// Here is a try/finally/except way of checking for errors. This
// will try to show the form. If there is an error, it will run the
// except code. The finally code will always be executed.
try
// Show the form.
SafeInitForm;
SafeShowFormModal;
finally
// Free the form from memory to avoid memory leaks.
FreeForm(frmDesign);
except
// Print an error message if by unfortunate chance that the form
// experiences an error.
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug; // Clear the debug box to make SCAR nice and clean.
MainInitForm; // Run the MainInitForm! :)
end.


~Ron :)

Smartzkid
04-13-2007, 08:27 PM
:D Nice tut...I was thinking about making a tut for this, but then I decided that it'd take too long...

If you wanna see an example of this in action, look here (v2 out soon):
SoundBoard (http://www.villavu.com/forum/showthread.php?t=8364)

Lalaji
05-24-2007, 07:42 PM
My head hurts, and i still dont get it :(. Guess i am not that advanced yet.

Great tut though