PDA

Click to See Complete Forum and Search --> : Drag'n'Drop


JP
Dec 2nd, 1999, 12:09 AM
I want to implement drag'n'drop in my app. I have two MDI child forms displayed with a grid on each. I would like to be able to drag the *value* from a cell on one form.grid and drop it onto the other. Anyone know of any articles that will put me on the right road (or even suggest how to do it)?

Thanks
JP

Aaron Young
Dec 2nd, 1999, 12:48 AM
Here's a quick Example..

Create 2 Forms, add an MSFlexGrid Control to Both, then..

In Form1..

Private Sub Form_Load()
'Setup the Grids
Dim X As Integer
Dim Y As Integer
Form2.MSFlexGrid1.FixedCols = 0
Form2.MSFlexGrid1.FixedRows = 0
With MSFlexGrid1
'Set the Drag Icon
.DragIcon = LoadPicture("..\Common\Graphics\Icons\DragDrop\Drag1pg.ico")
.FixedCols = 0
.FixedRows = 0
.Rows = 10
.Cols = 5
'Turn off Highlight, otherwise will select a bunch of Cells when Dragging
.HighLight = flexHighlightNever
'Fill Grid on Form1 with Dummy Data
For Y = 0 To 9
For X = 0 To 4
.TextMatrix(Y, X) = "Item " & ((Y * 5) + X)
Next
Next
End With
'Show the 2nd Form
Form2.Show
End Sub

Private Sub MSFlexGrid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Begin the Drag Process
If Button = vbLeftButton Then MSFlexGrid1.Drag vbBeginDrag
End Sub

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'End the Drag Process
If vbLeftButton Then MSFlexGrid1.Drag vbEndDrag
End Sub

In Form2..

Private Sub MSFlexGrid1_DragDrop(Source As Control, X As Single, Y As Single)
'Only Accept Dragged Items from a Grid
If TypeOf Source Is MSFlexGrid Then
With Source
'Load the Selected Cell with the Dragged Cell Text
MSFlexGrid1.Text = .TextMatrix(.Row, .Col)
End With
End If
End Sub

Private Sub MSFlexGrid1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
'This is the Tricky bit,
'You need to emulate Hot Tracking
'In order to know which cell you are
'Draggin over in the 2nd Grid
Dim iRow As Long
Dim iCol As Integer
If TypeOf Source Is MSFlexGrid Then
With MSFlexGrid1
'Figure out which Row we're over
For iRow = 0 To .Rows - 1
If .RowPos(iRow) <= Y And (.RowHeight(iRow) + .RowPos(iRow)) >= Y Then Exit For
Next
If iRow < .Rows Then .Row = iRow
'Figure Out Which Column We're Over
For iCol = 0 To .Cols - 1
If .ColPos(iCol) <= X And (.ColWidth(iCol) + .ColPos(iCol)) >= X Then Exit For
Next
If iCol < .Cols Then .Col = iCol
End With
End If
End Sub



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

JP
Dec 2nd, 1999, 06:04 PM
Thanks for this Aaron - it is a massive help! I am now modifying it slightly so that it works with TrueDBGrid Pro.

JP