Recent

Author Topic: [Solved] drag and drop treeview in stringgrid ?  (Read 831 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 512
[Solved] drag and drop treeview in stringgrid ?
« on: June 09, 2024, 10:33:41 am »
Hi
please help and guide me ?

how can i drag item of treeview in stringgrid and detected that ?

thank you
« Last Edit: June 09, 2024, 11:51:49 am by majid.ebru »

bobby100

  • Full Member
  • ***
  • Posts: 208
    • Malzilla
Re: drag and drop treeview in stringgrid ?
« Reply #1 on: June 09, 2024, 11:07:03 am »
I will try to help you and describe how I would do it.

You would need a global variable where you would put your data that you want to drag & drop.
Just for the sake of the example, let say we want to transport just a string, so I will name this variable MyTransportedString - declare it in the VAR section of the unit/form, and initialize it in the OnCreate of the Form1. For more complex data, you can use a Record.

Now, you need to process 3 procedures:
- TreeView1.StartDrag
- StringGrid1.DragOver
- StringGrid1.DragDrop

The StartDrag of the TreeView need to put the string in MyTransportedString:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1StartDrag(Sender: TObject; var DragObject: TDragObject);
  2. begin
  3.   //you need to check for errors here
  4.   //like checking if a node is selected at all (user can initiate drag operation even from blank space outside the nodes)
  5.   //and proceed just if the user have selected a valid node
  6.  
  7.   //after all the checks, copy the data from the node into our variable
  8.   MyTransportedString := TreeView1...Node...Text  //sorry, just pseudocode as I do not know TreeView
  9. end;

Now, you need to define from which components your StringGrid will accept the drag&drop
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DragOver(Sender, Source: TObject; X, Y: integer;
  2.   State: TDragState; var Accept: boolean);
  3. begin
  4.   if Source = TreeView1 then Accept := True
  5.   else Accept := False;
  6. end;

Now, process the drop on the StringGrid:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DragDrop(Sender, Source: TObject; X, Y: integer);
  2. var
  3.   DestCol, DestRow: integer;
  4. begin
  5.   StringGrid1.MouseToCell(X, Y, DestCol, DestRow);
  6.   //here do some basic error check, like not to accept the drop if the DestCol or DestRow are zero (fixed cols/rows) etc.
  7.   //if everything is OK then proceed
  8.   if (DestCol > 0) and (DestRow > 0) then
  9.     StringGrid1.Cells[DestCol, DestRow] := MyTransportedString;
  10. end;

Do not forget to set DragMode for both TreeView and StringGrid to dmAutomatic in Properties

majid.ebru

  • Hero Member
  • *****
  • Posts: 512
Re: drag and drop treeview on stringgrid ?
« Reply #2 on: June 09, 2024, 11:51:34 am »
@bobby100   thank you :) :) :) :)

bobby100

  • Full Member
  • ***
  • Posts: 208
    • Malzilla
Re: [Solved] drag and drop treeview in stringgrid ?
« Reply #3 on: June 09, 2024, 12:02:15 pm »
I have added some more comments to the code, please take a look.

Do a proper error checking, it is very important. I am working with ListBoxes, and most of the errors were about IndexOutOfBounds because user can start drag even if there was no item selected in the source ListBox (dragging from the blank space or from an unselected item).

majid.ebru

  • Hero Member
  • *****
  • Posts: 512
Re: [Solved] drag and drop treeview in stringgrid ?
« Reply #4 on: June 10, 2024, 10:20:26 am »
thank you

i can't select true node then i don't use this your code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1StartDrag(Sender: TObject; var DragObject: TDragObject);
  2. begin
  3.   //you need to check for errors here
  4.   //like checking if a node is selected at all (user can initiate drag operation even from blank space outside the nodes)
  5.   //and proceed just if the user have selected a valid node
  6.  
  7.   //after all the checks, copy the data from the node into our variable
  8.   MyTransportedString := TreeView1...Node...Text  //sorry, just pseudocode as I do not know TreeView
  9. end;

bobby100

  • Full Member
  • ***
  • Posts: 208
    • Malzilla
Re: [Solved] drag and drop treeview in stringgrid ?
« Reply #5 on: June 10, 2024, 04:49:54 pm »
I am afraid that I do not understand your message.

 

TinyPortal © 2005-2018