Results 1 to 3 of 3

Thread: Drag'n'Drop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 1999
    Posts
    16

    Post

    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

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Here's a quick Example..

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

    In Form1..
    Code:
    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..
    Code:
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 1999
    Posts
    16

    Post

    Thanks for this Aaron - it is a massive help! I am now modifying it slightly so that it works with TrueDBGrid Pro.

    JP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width