Recent

Author Topic: open a text file  (Read 827 times)

Paolo

  • Sr. Member
  • ****
  • Posts: 499
open a text file
« on: July 09, 2022, 10:40:30 am »
Hello,

I have a Tedit component showing a text file name (full path), something "c:\test.txt". I would like to put buttton close to the edit box and when clicked the text file automatically opens inside the default editor (Eg notepad, etc...) How can I achive this ?

thank you in advance.

OS: win 10-pro
LAZ: 2.2.0
FOC: 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: open a text file
« Reply #1 on: July 09, 2022, 11:53:01 am »
Use shellexecute in windows
You will need to include the windows unit.
For other os I don't know.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: open a text file
« Reply #2 on: July 09, 2022, 12:05:08 pm »
The cross-platform solution is OpenDocument from unit LclIntf:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   OpenDocument(Edit1.Text);
  7. end;

But be aware that this "opens" also exe files by executing them... What is the problem to add a memo to your form and load the file into it (Memo1.LoadFromFile(Edit1.Text))?

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: open a text file
« Reply #3 on: July 09, 2022, 03:43:05 pm »
The cross-platform solution is OpenDocument from unit LclIntf:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   OpenDocument(Edit1.Text);
  7. end;

But be aware that this "opens" also exe files by executing them... What is the problem to add a memo to your form and load the file into it (Memo1.LoadFromFile(Edit1.Text))?

Gotta admit I never thought of that one.

Does OpenDocument use the windows Shell ?

I know for HTML files it starts your browser.
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: open a text file
« Reply #4 on: July 09, 2022, 05:52:57 pm »
The cross-platform solution is OpenDocument from unit LclIntf:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   OpenDocument(Edit1.Text);
  7. end;

But be aware that this "opens" also exe files by executing them... What is the problem to add a memo to your form and load the file into it (Memo1.LoadFromFile(Edit1.Text))?

Gotta admit I never thought of that one.

Does OpenDocument use the windows Shell ?

I know for HTML files it starts your browser.

On Window$, it calls ShellExecuteW. Linux and macOS call RunCmdFromPath and FindFileNameOfCmd.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: open a text file
« Reply #5 on: July 10, 2022, 07:02:23 pm »
thank to all,

OpenDocument is perfect

@wp, no problem with TMemo, but this solution is ok for my needs.

 

TinyPortal © 2005-2018