Solved

Drag & Drop TDragObject

Posted on 2001-08-24
6
1,135 Views
Last Modified: 2012-08-14
Hi, just a little background! I have 3 types of controls

Tjob: an ancestor of Tstatictext
Tresource: an ancestor of Tpanel
TAdvStringGrid: an ancestor of tstringgrid (From RX suite)

Multiple instances of Tjobs can sit on multiple instances of TPanel.  TAdvStringGrid can also hold Tjobs in its objects array, and displays corresponding info on each row for each associated Tjob it has.  Basically I want to be able to drag Tjobs between the Tresource panels and the TAdvStringGrid.  Dragging a job to the grid isn't a problem, but I can't work out how I can drag the Tjob item corresponding to the row selection from the grid on to a TResource panel.  

TheTjob item I want to drag is given by:
AdvStringGrid.objects[0,AdvStringGrid.Row] as TJob;

The Tresource drag & drop handlers require a Tjob object, ie:

procedure TResource.ResourceDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
If Source is TJob then
   Accept:=IsCanAcceptJob(Source as TJob)
   else
   Accept:=false;
end;

(& similar for DragDrop)

So this is fine when dragging a Tjob on a Tresource, but when dragging from the grid, the Source becomes TadvStringGrid. Basically need to change it so that
Source is a specific TJob within the grid.

So do I need to create a TDragObject to do this and if so how should/can I pass the selected Tjob information within the TDragObject?  Hope u can help, and sorry this is a little long winded, although I'm sure you're used to my protracted explanations by now!!! Many thanks, P.
0
Comment
Question by:Pandora
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:Pandora
ID: 6422411
p.s. I don't want to reference the TAdvStringGrid in the Tresource class unit, ie I want the resource to stay ignorant of the state of the TAdvStringGrid altogether, if that makes sense!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6422965
?maybe

why not supply an additional attribute, maybe DragJob : TJob,
on the onstartdrag-event of the tjob and
like

-- in the type-declartion of your form
...
  private
     DragJob : TJob  public
   public
...

- in the Start of the TJOB

procedure TForm1.Job1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  DragJob := Self;
end;

- in the grid

procedure TForm1.AdvStringGrid1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  DragJob := HoweverYouKnowWhichTJobThisCurrentIs;
end;

- in your dragOver

procedure TResource.ResourceDragOver(Sender, Source: TObject; X,
        Y: Integer; State: TDragState; var Accept: Boolean);
begin
    Accept:= Assigned(DragJob) and (IsCanAcceptJob(DragJob))
end;

- in your dragdrop
...
  //your stuff you will do with TJob=DragJob
  DragJob := Nil;
...

- you may also set it to nil, if the dragopoeration is canceled

meikl ;-)
0
 
LVL 1

Author Comment

by:Pandora
ID: 6424121
Thanks Meikl very cunning! - sometimes I wonder why I don't just address my questions to you personally!!  

Just for PAQ refs, Because the TResource is a seperate unit (not referencing the main form in the uses clause) I have put a global var that equates to the DragDrop var used above in the Tjob unit instead - this was already referenced by the Tresource unit and the main form (holding the TAdvStringGrid) and so the change doesn't require extra units to be added to the TResource unit uses clause.

Meikl, could I ask you a related quicky (I've increased points to 300 to compensate!).  I'm having problems with drag-drop generally (particularly on the TAdvStringGrid) because the OnStartDrag event fires before other events such as the OnSelectCell & OnRowChanging events.  As such when I try to reference the AdvStringGrid.row property in the OnStartDrag proc, the row being dragged is not the same as the 'selected' row.  How can I ensure that these are the same?  I think I effectively need to call the OnSelectCell event in/before the OnstartDrag.  Should I be cunningly alternating the drag mode between dmmanual to dmautomatic to stop the OnstartDrag happening when the row is just being selected? but then I still want to be able to drag & drop as well as select. Agggh! Hope you can help this poor bewildered soul, many thanks again, P.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 6424503
hi,

i didn't have this grid,
but i guess there may a mousetocell-method,
then you could use soemthing like this
(sample with a tstringgrid, maybe applyable to the tadvstringgrid)

procedure TForm1.StringGrid1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
var P : TPoint;
    ACol, ARow : Integer;
begin
  GetCursorPos(P);  //where is the mouse
  P := StringGrid1.ScreenToClient(P);  //transform to relative coords
  StringGrid1.MouseToCell(P.x,P.y,ACol,ARow); //transform to gridccoords
  StringGrid1.Col := ACol;    //select the cell
  StringGrid1.Row := ARow;
  SHowMessage('Starting Drag');  //not needed
  //assign your dragjob now
end;


meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6424572
o yeah,

you wrote it in your q
(AdvStringGrid.objects[0,AdvStringGrid.Row] as TJob;)

procedure TForm1.StringGrid1StartDrag(Sender: TObject;
var DragObject: TDragObject);
var P : TPoint;
     ACol, ARow : Integer;
begin
  GetCursorPos(P);  //where is the mouse
  P := StringGrid1.ScreenToClient(P);  //transform to relative coords
  StringGrid1.MouseToCell(P.x,P.y,ACol,ARow); //transform to gridccoords
  StringGrid1.Col := ACol;    //select the cell
  StringGrid1.Row := ARow;
  {
  //now you assign here
  DragJob := AdvStringGrid.objects[0,ARow] as TJob;
  }
end;

meikl ;-)
0
 
LVL 1

Author Comment

by:Pandora
ID: 6424869
Aha! Thanks Meikl, thats done the trick! Best wishes, P :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

18 Experts available now in Live!

Get 1:1 Help Now