Solved

Drag and drop

Posted on 1998-02-12
4
242 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now