Solved

Drag and drop

Posted on 1998-02-12
4
246 Views
Last Modified: 2010-05-18
I am trying to implement drag and drop between DBGrids on two seperate forms but as yet I haven't managed it. Can someone help me?
0
Comment
Question by:alanward
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:d003303
ID: 1359065
do the two forms belong to the same process (i.e. the same application instance) ? If not, you will have to implement OLE drag and drop to transfer data between process boundaries.

Slash/d003303
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1359066
Here is an example :

Push Fill grid button; (bitbutton1)
Push Show other form button (button1)

Push mouse button on grid and drag to other form

Regards, Zif.

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    Button1: TButton;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 Form2.Show;
end;

procedure TForm1.Button1Click(Sender: TObject);

var i,j : integer;
begin
 for i := 1 to 10 do
  for j := 1 to 10 do
   StringGrid1.Cells[i,j] := 'Cell '+IntToStr(i)+IntToStr(j);
end;

end.

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure StringGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.StringGrid1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if Source is TStringGrid then
    Accept := True;end;

procedure TForm2.StringGrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source is TStringGrid then
   begin    StringGrid1.Cells[Form1.StringGrid1.Col,Form1.StringGrid1.Row] :=      Form1.StringGrid1.Cells[Form1.StringGrid1.Col,Form1.StringGrid1.Row];   end;
end;

end.

Questions? Just ask.

0
 

Author Comment

by:alanward
ID: 1359067
ZifNab's answer involved String grids my question was specifically about DBGrids, I don't know if the same code will work for both.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1359068
sure it works with dbgrids! Only the dragdrop is different :
With this example you can see that the dbgrid in form2 will accept from dbgrid1.

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    DBGrid1: TDBGrid;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Form2.Show;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 dbgrid1.dragmode :=  dmAutomatic;
end;

end.

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids;

type
  TForm2 = class(TForm)
    DBGrid1: TDBGrid;
    procedure DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
if Source is TDBGrid then
            Accept := True;
end;


end.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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