954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How Do You Open Files With Buttons

Hi, i'm new to these forums and i was wondering, what kind of script or code do you need to write in order to make a button open a specific file, such as a pdf or txt, and what code or script do you need to make a button open to another window that you designed?

i'm completely new to all this Delphi, C, C++ stuff and have no prior experience whatsoever. I am doing this as a personal project, so i can try and learn more about it and get better.

bigmike9449
Newbie Poster
2 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Hello,

Opening an ANSI TXT file is easy as you can load it to TStringList, memory stream, TMemo component, etc. But everything depends on a particular task.

If you need to just display a TXT file in a TMemo component, the code may look like:

if not OpenDialog1.Execute then Exit;
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);

Reading a PDF file is not an easy task as PDF is a binary format.
I suppose you will have to use a PDF ActiveX control to display it, so it depends on the methods the control provides.

As to the secondary form, you will need to just open the form first and then display the file there. Thought, I prefer to create a secondary form dynamically (not in the DPR unit.)

For example:

MyForm:= Application.CreateForm(TMyForm, MyForm);
try
  if not OpenDialog1.Execute then Exit;
MyForm.Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
  if MyForm.ShowModal <> mrOk then Exit;
  // some code here
finally
  MyForm.Free;
end;

HTH.
Tom Barnet
HelpSmith 2.0 is Released. Have You Yet Upgraded?

Tom Barnett
Newbie Poster
4 posts since Sep 2008
Reputation Points: 13
Solved Threads: 0
 

For opening the external file (txt, pdf) if you want it to open in it's default program (e.g. notepad or Adobe Reader) you can use the ShellExecute API call.

uses ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Shellexecute(Handle,'open',INSERT_FILE_PATH_HERE,nil,nil,SW_SHOWNORMAL);
end;
espSquall
Newbie Poster
10 posts since Sep 2008
Reputation Points: 10
Solved Threads: 1
 

For opening the external file (txt, pdf) if you want it to open in it's default program (e.g. notepad or Adobe Reader) you can use the ShellExecute API call.

uses ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Shellexecute(Handle,'open',INSERT_FILE_PATH_HERE,nil,nil,SW_SHOWNORMAL);
end;

this is probably what i was looking for, thanks; i tried using this but i kept getting errors, something about "[Error] Unit1.pas(37): Undeclared identifier: 'Shellexecute'"

can you help?

bigmike9449
Newbie Poster
2 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Have you added "ShellAPI" to the uses section?

espSquall
Newbie Poster
10 posts since Sep 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You