Recent

Author Topic: How to attach files (eg. PDF) in TMemo? Is it posible?  (Read 1717 times)

rdny

  • New Member
  • *
  • Posts: 38
How to attach files (eg. PDF) in TMemo? Is it posible?
« on: December 14, 2018, 01:30:00 am »
How to attach files (eg. PDF) in TMemo? Is it posible? Like Dragging it in Tmemo and it will appear there. Thanks guys!

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to attach files (eg. PDF) in TMemo? Is it posible?
« Reply #1 on: December 14, 2018, 03:07:53 am »
It is possible. What it is not is "easy", except when the files you drop are text files.

Assuming you are drag-dropping from, say, a file manager to your application, follow these steps:
  • In your form set AllowDropFiles to True, either in the Object Inspector or in code
  • Write a handler for the form's OnDropFiles event that either loads the file(s) into the memo(s) (with Memo.LoadFromFile) or loads each file(s) into a string list and inserts it in the memo, like:
      StringList.LoadFromFile(FileName[ i ]);
      Memo.SelText := StringList.Text;

    or whatever else you want to do with it.

If, however, you drop any file whose contents are not mainly text, you'll first have to interpret the file to convert it into text.

HTH
« Last Edit: December 14, 2018, 03:09:30 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

rdny

  • New Member
  • *
  • Posts: 38
Re: How to attach files (eg. PDF) in TMemo? Is it posible?
« Reply #2 on: December 14, 2018, 07:38:54 am »
It is possible. What it is not is "easy", except when the files you drop are text files.

Assuming you are drag-dropping from, say, a file manager to your application, follow these steps:
  • In your form set AllowDropFiles to True, either in the Object Inspector or in code
  • Write a handler for the form's OnDropFiles event that either loads the file(s) into the memo(s) (with Memo.LoadFromFile) or loads each file(s) into a string list and inserts it in the memo, like:
      StringList.LoadFromFile(FileName[ i ]);
      Memo.SelText := StringList.Text;

    or whatever else you want to do with it.

If, however, you drop any file whose contents are not mainly text, you'll first have to interpret the file to convert it into text.

HTH


That's right, what I am getting is just a text and I can't open it. I just need to send files like it is a messenger. Thank you lucamar! :D

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: How to attach files (eg. PDF) in TMemo? Is it posible?
« Reply #3 on: December 14, 2018, 09:03:16 am »
Please, it's not possible. TMemo requires only text, nothing more.
Converting a PDF is not what you want. Even pictures are not allowed to load into TMemo the right way.

If you look at your mail messenger, you see a listview with only the filename. When this listbox is filled, messenger know there are attached files to sent. It takes the file into a stream, convert it into text and send the attachment to the receiver.

I created a simple mail messenger in our application with indy to do it for the user without knowing how it works.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to attach files (eg. PDF) in TMemo? Is it posible?
« Reply #4 on: December 14, 2018, 02:43:07 pm »
That's right, what I am getting is just a text and I can't open it. I just need to send files like it is a messenger. Thank you lucamar! :D

Then change the response in the OnDropFiles to add the file names in another control, not the memo. Something like a TListView or similar should suffice.

As an idea, if it's a text file, ask the user what she wants to do, whether to add/insert the text into the message, replace the message or add the file as attachment. Some people like to compose messages in external editors or use boilerplate messages: offering an alternative to copy-paste would be nice :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018