Board index » cppbuilder » BCB4: Dragging from a ListView to a StringGrid

BCB4: Dragging from a ListView to a StringGrid

Does anyone know how I could drag the caption of a TListView(vsReport
View)'s ListItem and drop it into a cell of a StringGrid?

Any help/examples will be greatly appreciated.

~Josh

 

Re:BCB4: Dragging from a ListView to a StringGrid


Hi Josh,

Quote
> Does anyone know how I could drag the caption of a TListView(
> vsReport View)'s ListItem and drop it into a cell of a StringGrid?

Here's an example:  Set the ListView's DragMode property to dmAutomatic
(and the DragKind property to dkDrag), and then use the following code
for the StringGrid's OnDragOver event handler...

void __fastcall TForm1::StringGrid1DragOver(
    TObject *Sender,
    TObject *Source,
    int X, int Y,
    TDragState State,
    bool &Accept
   )
{
   if (Source == ListView1)
   {
      int col = -1, row = -1;
      StringGrid1->MouseToCell(X, Y, col, row);
      Accept =
         col != -1 && row != -1 &&
         col >= StringGrid1->FixedCols &&
         row >= StringGrid1->FixedRows;

      if (Accept)
      {
         StringGrid1->Col = col;
         StringGrid1->Row = row;
      }
   }

Quote
}

The OnDragOver event handler is called whenever another VCL object is
being dragged over the StringGrid.  It's from within this member
function that you decide whether or not to accept the drop (by changing
the Accept parameter).  Here I make sure that the object being dragged
(Source) is the ListView, and then I make sure that the mouse cursor (X,
Y) is currently over a valid cell.

Next, use the following code for the StringGrid's OnDragDrop event
handler...

void __fastcall TForm1::StringGrid1DragDrop(
    TObject *Sender,
    TObject *Source,
    int X, int Y
   )
{
   const int col = StringGrid1->Col;
   const int row = StringGrid1->Row;
   StringGrid1->Cells[col][row] =
      ListView1->Selected->Caption;

Quote
}

The OnDragDrop event handler is called when the user drop the dragged
object on the StringGrid.  This event handler will fire only if the
previous OnDragOver event handler indicated that it could accept the
drop.  Here, I simply copy the text in the first-column of the
ListView's selected item to the StringGrid's current cell.  (If you want
to copy the text of ListItem's entire row, just iterate over the
TListItem::SubItems property.)

Good luck!
--
Damon Chandler (TeamB)
- BCBCAQ <http://bcbcaq.freeservers.com>
- Graphics API Black Book <http://graphicsBB.itgo.com>

Re:BCB4: Dragging from a ListView to a StringGrid


"Damon Chandler (TeamB)" <dm...@cornell.edu> wrote in message
news:3B521466.657B9D6B@cornell.edu...
Quote
> Here's an example:

[snip]

Thanks!  That appears to work fine.  (However, I seem to have a knack for
screwing up sure things... :) )

Other Threads