Step 3: Manual OLE Drag

We will customize the behavior of the fgDDManual control as an OLE drag source in two ways:

1.   We will initiate dragging whenever the user clicks on the current cell, and

2.   We will add a copyright notice to the contents being dragged from the control.

Because the OLEDragMode property of the fgDDManual control is set to flexOLEDragManual, you need to initiate the OLE dragging operation with code, using the OLEDrag method. To do this we will add code to handle the BeforeMouseDown event. When the user clicks on the active cell, we call the OLEDrag method. Here is the code:

    Private Sub fgDDManual_BeforeMouseDown(ByVal Button As

                  Integer, _

                  ByVal Shift As Integer, _

                  ByVal X As Single, ByVal Y As Single, _

                   Cancel As Boolean)

        With fgDDManual

   

            ' if the click was on the active cell

            ' start dragging

            If .MouseRow = .Row And .MouseCol = .Col Then

       

                ' use OLEDrag method to start manual

                ' OLE drag operation

                ' this will fire the OLEStartDrag event,

                '  which we will use

                ' to fill the DataObject with the data we

                ' want to drag.

 

                .OLEDrag

       

                ' tell grid control to ignore mouse

                ' movements until the

                ' mouse button goes up again

                Cancel = True

            End If

        End With

    End Sub

The code above checks whether the user clicked on the active cell. If so, it calls the OLEDrag method and sets the Cancel parameter to True.

Note that we have not specified what the data is. In automatic mode, the control assumed that you wanted to drag the current selection. In manual mode, you are responsible for providing the data.

When the OLEDrag method is called, the control fires the OLEStartDrag event, which gives you access to a DataObject object. You must store the data that will be dragged into the DataObject so that the target object can get to it. Here is the code:

    Private Sub fgDDManual_OLEStartDrag(Data As VSFlex8Ctl.vsDataObject, AllowedEffects As Long)

   

        ' set contents of data object for manual drag

        Dim s$

        s = fgDDManual.Clip & vbCrLf & "Copyright 2000 ComponentOne"

        Data.SetData s, vbCFText

   

    End Sub

The code takes the current selection (contained in the Clip property), appends a copyright notice to it, and then assigns it to the Data parameter. This is the data that will be exposed to the OLE drop targets.

If you run the project now, and type some data into the fgDDManual control, you will be able to drag it to one of the other controls on the form. Notice how the copyright notice gets appended to the selection when you make the drop.