Recent

Author Topic: add txt file contents to memo  (Read 19628 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
add txt file contents to memo
« on: June 10, 2013, 06:44:34 pm »
My little program can currently open a directory with DirectoryEdit, display its contents in a FileListBox and doubleclick a line to show the name in a showmessage.
All very basic.

I would like to be able to doubleclick the filename and open the file to display its contents in a memobox or similar for editing.
The file will only be .txt.
Can anyone offer any help ?
Thanks.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, ListFilterEdit, Forms, Controls, Graphics,
  Dialogs, StdCtrls, EditBtn, FileCtrl;

type

  { TForm1 }

  TForm1 = class(TForm)
    DirectoryEdit1: TDirectoryEdit;
    FileListBox1: TFileListBox;
    Memo1: TMemo;
    procedure DirectoryEdit1Change(Sender: TObject);
    procedure FileListBox1DblClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.DirectoryEdit1Change(Sender: TObject);
begin
  FileListBox1.Directory:=DirectoryEdit1.Directory;
end;

procedure TForm1.FileListBox1DblClick(Sender: TObject);
begin
  showmessage(extractFileName(FileListbox1.filename));
end;

end.
   

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #1 on: June 10, 2013, 07:03:04 pm »
Code: [Select]
procedure TForm1.FileListBox1DblClick(Sender: TObject);
begin
  //showmessage(extractFileName(FileListbox1.filename));
  Memo1.Lines.LoadFromFile(FileListbox1.filename);
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: add txt file contents to memo
« Reply #2 on: June 10, 2013, 07:08:08 pm »
Perhaps you will must convert UTF8 filenames:

Code: [Select]
Memo1.Lines.LoadFromFile(UTF8ToAnsi(FileListBox1.FileName));

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #3 on: June 10, 2013, 07:39:59 pm »
No, Filename is of type string not utf8string this means that if there was a conversion needed it should have already been converted before it entered the file list. Farther more it would be problematic if every single component in the lcl used its own encoding, it paramount to use a single encoding for all the lcl otherwise it creates a lot of uncertainty in your code or simple put, it is a design error to have multiple encodings of strings.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: add txt file contents to memo
« Reply #4 on: June 10, 2013, 07:46:31 pm »
I only can load a memo with accented characters on filenames if I use UTF8ToAnsi.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #5 on: June 10, 2013, 07:58:46 pm »
depends how you got the filename it self, a constant for example, might need that if the source file is saved as a UTF8 if it saved as an ANSI then it shouldn't need it. In any case getting the filename from any lcl component should not require the use of string conversion it should be already in the default lcl string format for the widgetset at hand in case of windows and lcl 1.0.8 and FCP2.6.2 that should be ANSI.

In any other case it is a bug that need to be reported, keep in mind that utf8 specific component are excluded from the above statement but at the same time must be clearly marked as such and they must not be confused for the stock components of lcl.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: add txt file contents to memo
« Reply #6 on: June 11, 2013, 11:23:46 am »
Thanks Taazz, that's helped a lot and moved me on.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: add txt file contents to memo
« Reply #7 on: June 17, 2013, 12:47:56 am »
I now have the program working to locate a directory, list the .txt files and then display the contents of the selected file in a memobox.
How can I save changes made in the memobox directly to the text file ?
Is this possible or do I have to open the file and perform a save ?

Thanks.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #8 on: June 17, 2013, 01:37:21 am »
memo1.llines.savetofile(FileListbox1.filename);
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: add txt file contents to memo
« Reply #9 on: June 17, 2013, 01:49:53 am »
memo1.llines.savetofile(FileListbox1.filename);

As simple as that ?
Thanks big time.
I'll have to add an "are you sure" option methinks.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #10 on: June 17, 2013, 01:53:37 am »
Yes the command will replace the contents of the file with what ever memo1 has at that time.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: add txt file contents to memo
« Reply #11 on: June 17, 2013, 03:04:44 am »
What would the best method be to prevent a user changing the selection in filelistbox if a change has been made in memo.
Currently I enable a button on memo change and buttonclick saves the changes. However, the filelist selection can be changed and the save would save to the new selected file and not the one actually loaded.
Removing the cursor doesn't prevent item selection.
I'm assuming a yes/no dialog window would be an answer but I'm still unsure what event I should assign this to to catch it.
I don't understand the EditDone event which, on the surface, seems the one to choose.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: add txt file contents to memo
« Reply #12 on: June 17, 2013, 03:30:37 am »
how about after you load the file in the memo you set an internal variable with the filename used to load the data and use that instead of the filebox1.filename?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: add txt file contents to memo
« Reply #13 on: June 17, 2013, 03:39:14 am »
how about after you load the file in the memo you set an internal variable with the filename used to load the data and use that instead of the filebox1.filename?

That's exactly what I did and it works a treat.
Now I need to stop the memo showing as changed when it loads the first text file.
I suppose it could load the first file on startup but that's not ideal.

CM630

  • Hero Member
  • *****
  • Posts: 1401
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: add txt file contents to memo
« Reply #14 on: June 18, 2013, 07:50:15 am »
I only can load a memo with accented characters on filenames if I use UTF8ToAnsi.
I usually use UTF8toSys, without it nothing works in 90% of the cases.

For example the following code does not work, because the file cannot be found
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  OpenDialog: TOpenDialog;
begin
  OpenDialog:= TOpenDialog.create(self);
  if OpenDialog.Execute then Memo1.Lines.LoadFromFile(OpenDialog.FileName);
end;
while
Code: [Select]
Memo1.Lines.LoadFromFile(UTF8ToSys(OpenDialog.FileName)); is quite okay.

Code: [Select]
Memo1.Lines.LoadFromFile(FileListbox1.filename); cannot find the file, while
Code: [Select]
  Memo1.Lines.LoadFromFile(UTF8ToSys(FileListbox1.filename)); finds it.


Also, .LoadFromFile is not much usable. It is okay for UTF8 files, but for other cases I recommend using utf8tools.
« Last Edit: June 18, 2013, 08:23:00 am by paskal »
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018