Solved

TMemo drag and drop

Posted on 2004-03-30
13
778 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 125 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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