?
Solved

TMemo drag and drop

Posted on 2004-03-30
13
Medium Priority
?
894 Views
Last Modified: 2010-04-16
Supposed to be easy: how to drag and drop a file from windows explorer in a TMemo?

Thank you!
0
Comment
Question by:__alex
  • 7
  • 3
  • 2
  • +1
13 Comments
 

Expert Comment

by:rafamvc
ID: 10714620
why tmemo? are u planning to receive a txt file?
0
 
LVL 2

Author Comment

by:__alex
ID: 10714671
For the sake of simplicity: Yes.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 500 total points
ID: 10714862
NOTE:  Uses SHellAPI

Shane



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShellApi;


type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure WMDROPFILES(var Message: TWMDROPFILES); message WM_DROPFILES;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
  NumFiles : longint;
  i : longint;
  buffer : array[0..255] of char;
begin
 {How many files are being dropped}
  NumFiles := DragQueryFile(Message.Drop,
                            0,
                            nil,
                            0);
 {Accept the dropped files}
  for i := 0 to (NumFiles - 1) do begin
    DragQueryFile(Message.Drop,
                  i,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.Add(buffer);
  end;

end;


procedure TForm1.FormCreate(Sender: TObject);
begin
 {Let Windows know we accept dropped files}
  DragAcceptFiles(Form1.Handle, True);
end;

end.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 10715522
shane,
i guess alex wants to open the file which was dropped
(in case of a text-file),
not a filelist

still listening . . .

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10715555
Have you tried the code meikl,

it will allow you to drag a text file and drop it into your TMemo.
I didn't provide the Error checking, so this assumes that the file is a Text File

Shane
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10715974
no, i didn't tried, i guess it works,
but i see that you fill the memo with the dropped filenames,
and i guess, alex want to fill the memo with content of a dropped text-file

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716069
Oh, I see .. i had a brain fart - <smile>

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716083
So I guess he could alter it,  to load the file versus add the file name to it.

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716103
Off the top of my head, something like:

Shane

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
   buffer : array[0..255] of char;
begin
   DragQueryFile(Message.Drop,
                  i,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.LoadFromFile(buffer);
  end;

end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716127
sorry

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
   buffer : array[0..255] of char;
begin
   DragQueryFile(Message.Drop,
                  0,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.LoadFromFile(buffer);
end;

Shane

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10717090
yep, only a smart change ;-))

but i owuld left out form1 here

>Form1.Memo1.Lines.LoadFromFile(buffer);

just

>Memo1.Lines.LoadFromFile(buffer);

(becaue you are in the TForm-object with this method,
whereas form1 may point to another instance,is  nil or points to somewhere)

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10717147
yeah, i type faster than i think...<smile>

Shane
0
 
LVL 2

Author Comment

by:__alex
ID: 10721490
Works great, thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question