Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drag and drop help 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a form that I have several string grids on. One string grid is filled with information that I would like to have the users drag to one of the other grids. I have looked at the help on the DragOver and DragDrop, but can't figure out how to get the information out!

Here's what I've got so far:
Code:
procedure TfrmSelectJury.sgJurorsDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TStringGrid;
end;

procedure TfrmSelectJury.sgJurorsDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if (Sender is TStringGrid) and (Source is TStringGrid) then
  begin
    with Sender as TStringGrid do
    begin
      //here I would like to take the value of columns from the first grid (sender) and put them in columns in the second grid (source)

    end;

  end;

end;

Can anyone tell me what I need to do in order to extract information from the specific columns?

Thanks

Leslie
 
Suppose you want to copy over column 1 of your source string grid into the destination string grid then it really is as simple as:
Code:
procedure TForm1.g3DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  src: TStringGrid;
  dst: TStringGrid;
begin
  if (source is TStringGrid) and (sender is TStringGrid) then begin
    src := source as TStringGrid;
    dst := sender as TStringGrid;
    dst.cols[1].Assign( src.Cols[1] );
  end;
end;
Note that you must use Assign rather than
Code:
dst.cols[1] := src.cols[1];

Andrew
 
What if I just want the value of a specific cell? So if my first string grid is:

Number Name ID
1
2 Jane Doe 365
3 John Smith 266
4
5 Adam Jones 699

And the user clicks on Jane Doe and drags it to a second grid where the results would be:

ID Name
365 Jane Doe

Then the user clicks on Adam Jones and drags to the second grid where it would now look like:


ID Name
365 Jane Doe
699 Adam Jones

Can I do something like this?

Thanks,

Leslie

 
The OnMouseDown event handler for the source string grid will give you the mouse X, Y coordinates. These can be translated into the cell column and row coordinates by calling the MouseToCell function.

Once you know which cell was dragged to the destination string grid the rest is dependant on your particular requirements but should be straightforward.

Andrew
 
Ok, thanks I'll work on it today and see if I can get that to work!

Leslie
 
Do I call the OnMouseDown Event and the MouseToCell function IN the DragDrop procedure or somewhere else? Sorry I'm being so dense, I'm just having problems getting my head around this!

leslie
 
One rarely calls event handlers directly.

I think you would assign a method to the OnMouseDown event handler that stores the most recent click to some TForm fields, and then reference those fields in the DragDrop event handler.

Alternatively, you can use the Windows API to get the mouse position and convert it into local coordinants (e.g. the ScreenToClient function).

Cheers
 
Ok, so I created a variabled named SelectedRow that I populate in the OnMouseDown event of the stringgrid. however, I have to press the right mouse button in order to get my variable set. How can I change it to respond to the left mouse button? Here's what I've got so far:

Code:
//get selected row information
procedure TfrmSelectJury.sgTrialPanelistsMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Column, Row: Longint;
begin
  sgTrialPanelists.MouseToCell(X, Y, Column, Row);
  Self.SelectedRow := Row;
end;

//in the drag drop procedure, fill the destination grid with the correct information from the source grid

procedure TfrmSelectJury.sgJurorsDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  src: TStringGrid;
  dst: TStringGrid;
  i : integer;
begin
  if (source is TStringGrid) and (sender is TStringGrid) then begin
    src := source as TStringGrid;
    dst := sender as TStringGrid;
    for i := 1 to dst.RowCount do
    begin
      if dst.Cells[0, i] = '' then
      begin
        dst.Cells[1, i] := src.Cells[1, Self.SelectedRow];
        dst.Cells[0, i] := src.Cells[2, Self.SelectedRow];
        Break;
      end;
    end;
  end;
end;

Now this works as long as I right click with the mouse and then left click to start the drag and I'd like to have it happen in a single operation, press the left mouse button down to start the drag, get the information I need (either the selected row or the information in the cell) and drag to the other grid.

I have tried using the startdrag, but it doesn't pick up the row I've selected, just the first row.

Any ideas?

Thanks!

leslie
 
I think that you need to use DragMode dmManual rather than dmAutomatic.

Set the DragMode property of your sgTrialPanelists StringGrid to dmManual.

Write an OnSelectCell event handler for sgTrialPanelists along the lines of:
Code:
procedure TfrmSelectJury.sgTrialPanelistsSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  Self.SelectedRow := ARow;
  sgTrialPanelists.BeginDrag (false,5);
end;
The OnMouseDown event handler from the previous effort is no longer required.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top