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.

Recommended Answers

All 4 Replies

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?

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;

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?

Have you added "ShellAPI" to the uses section?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.