Contact Sales

Call center available M-F 9:00 - 4:00 US Eastern Time.

Case Studies

Datasoft Solutions

Datasoft Solutions selected Xtreme Toolkit Professional with Visual C++ over many...

Adding Drag and Drop Support

Author: Mike Palmatier
Posted: May 3, 2008
Environment: Visual Basic 6.0

Downloads:
 DragandDropSample.zip - Source Files with Demo Project [4 KB]

To enable drag and drop in a report control only a single line of code is needed. The EnableDragDrop method enables drag and drop for a report control. You must use EnableDragDrop for each report control that will use drag and drop. The report control will handle everything else for you as long as you are dragging a row between report controls.

Use the EnableDragDrop method to enable drag and drop support for each report control that will use drag and drop. You need to use flags from the XTPReportDragDrop enumeration to specify which drag and drop operations that the report control will support. This is all you need to drag and drop rows between report controls. All data handling is handled internally.

    Dim cfRecords As Integer

 

    'Below, the clipboard string is "ReportSample:frmDragDrop" and we are allowing all drag and drop operations

    cfRecords = wndReport.EnableDragDrop("ReportSample:frmDragDrop", xtpReportAllowDrag Or xtpReportAllowDrop)

If you need to drag and drop between a report control and another control\object that is not a report control a few more steps are required and you will need to do the data handling yourself. The sample below illustrates dragging item between a report control and a list control. Basically all you need to do is set up the OLE events of the list control so you can format the data into the correct format for the report control and list control. The report control also has BeginDrag and DropRecords events.

Below is the code used to implement drag and drop to\from a report control and a list control:

Option Explicit

 

'An integer value that will be used to indicate whether an item in the DataObject object matches the

'specified ClipboardString(Parameter in EnableDrageDrop) format.  The DataObject is a parameter in OLEDrag events for controls

'that contains the data on the clipboard.

Dim cfRecords As Integer

 

Private Sub Form_Load()

    'Adds 3 items to the List Control

    lstItems.AddItem "Item 1"

    lstItems.AddItem "Item 2"

    lstItems.AddItem "Item 3"

 

    'Do not allow columns to be removed from the Report Control

    wndReport.AllowColumnRemove = False

 

    'Add a column named "Items" to the Report Control

    wndReport.Columns.Add 0, "Items", 50, True

 

    Dim str As String, i As Long

 

    'Add 4 records\rows to the Report Control

    For i = 4 To 8

        Dim Record As ReportRecord

        Dim Item As ReportRecordItem

 

        Set Record = wndReport.Records.Add

 

        str = "Item " & CStr(i)

        Set Item = Record.AddItem(str)

    Next i

 

    'Adds the records the Report Control

    wndReport.Populate

 

    'To enable Drag and Drop in a Report Control the EnableDragDrop method must be used. EnableDragDrop

    'does two things, first it sets a clipboard string that will be used to indicate the type of data

    'that is copied to the clipboard.  Second, it sets the drag and drop effects that are allowed when

    'dragging items to\from the report control.  The available effects are stored in the XTPReportDragDrop

    'enumeration.  If dragging items to\from one or more Report Control's , the same clipboard string must

    'be used for all Report Control's when calling EnableDragDrop.

    '

    'Below, the clipboard string is "ReportSample:frmDragDrop" and we are allowing all drag and drop operations

    cfRecords = wndReport.EnableDragDrop("ReportSample:frmDragDrop", xtpReportAllowDrag Or xtpReportAllowDrop)

End Sub

 

Sub ClearListSelection()

    Dim i As Long

    For i = 0 To lstItems.ListCount - 1

        lstItems.Selected(i) = False

    Next

End Sub

 

'The Mouse_Mouve event will be used to prepare records to be dropped into the report.

Private Sub lstItems_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

 

    'If the left mouse button is pressed while in the List Control

    If (Button And vbLeftButton) Then

 

        Dim Records As ReportRecords

 

        'CreateRecords is primarily used to create an empty ReportRecords collection in Drag and Drop

        'operations.  The new ReportRecords collection is used to create a collection of ReportRecord

        'objects to be dropped into the Report Control or to retrieve records that have been dragged

        'out of the Report Control.

        '

        'Below, a records collection is created that will contain a record for each item that

        'is currently selected in the list control

        Set Records = wndReport.CreateRecords

 

        Dim i As Long

 

        'Loop through all items in the List Control

        For i = 0 To lstItems.ListCount - 1

 

            'If the list item is selected, then add it to then create a record for it

            'in the records colleciton

            If lstItems.Selected(i) Then

 

                Dim Record As ReportRecord

 

                'Add a new record to the records collection

                Set Record = Records.Add

                Record.AddItem lstItems.List(i)

 

                Debug.Print "Added " & i

            End If

        Next

 

        'The DragRecords method is used to prepare records to be dropped into the Report Control.

        'DragRecords accepts a ReportRecords collection created by CreateRecords.  Any records that

        'have been prepared using DragRecords will be added to the Report when the items are dropped

        'into the Report Control.  Typically DragRecords would be used in the MouseMove event of the

        'control from which items are being dragged.  The Report Control will automatically add the

        'new records to the report once they have been dropped.

        '

        'Below, the Records collection is filled with a record that corresponds to each item that is

        'currently selected in the List Control.  If the items are dropped into the Report Control, then

        'all records contained in the Records collection will be added to the report.

        '

        'Now Data.GetFormat(cfRecords) will return True as some Records have been added to the clipboard

        wndReport.DragRecords Records

 

    End If

End Sub

 

'The OLEDragDrop event will be used to add items to the List Control that have been dragged from the Report Control

Private Sub lstItems_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)

 

    'If the format of the data on the clipboard is of type "ReportSample:frmDragDrop" as set with EnableDragDrop

    If Data.GetFormat(cfRecords) Then

 

        'Create a ByteArray

        Dim byteData() As Byte

 

        'Retrieve the Records collection from the clipboard, the data is stored as a ByteArray

        byteData = Data.GetData(cfRecords)

 

        Dim Records As ReportRecords

 

        'The CreateRecordsFromDropArray method is used when retrieving records that have been dragged

        'from the Report Control.  CreateRecordsFromDropArray accepts the ByteArray returned from the

        'Data.GetData parameter from any control's OLEDragDrop event and populates a ReportRecords

        'collection created by CreateRecords with the records stored on the clipboard.

        '

        'Below, the Records collection is populated by the records that were copied to the clipboard

        Set Records = wndReport.CreateRecordsFromDropArray(byteData)

 

        'If there were some records on the clipboard

        If (Not Records Is Nothing) Then

 

            'Unselect all items in the list control

            ClearListSelection

 

            Dim i As Long

 

            'Add a list item for each record that was on the clipboard

            For i = 0 To Records.Count - 1

                lstItems.AddItem Records(i).Item(0).Value

                lstItems.Selected(lstItems.ListCount - 1) = True

            Next

 

        End If

 

        'If the ctrl key was pressed when the items were dropped, then the drag operation was a Copy

        'Else, a Move operation is performed

        If ((Shift And 2) = 0) Then

            Effect = vbDropEffectMove

        Else

            Effect = vbDropEffectCopy

        End If

 

    End If

 

End Sub

 

'The OLEDragOver event is used to detect when items are dragged over the list control

Private Sub lstItems_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)

    Effect = 0

 

    'If the format of the data on the clipboard is of type "ReportSample:frmDragDrop" as set with EnableDragDrop

    If Data.GetFormat(cfRecords) Then

 

        'If the ctrl key is pressed when items are dragged over the list control, then the drag operation is Copy

        'Else, a Move operation is performed

        If ((Shift And 2) = 0) Then

            Effect = vbDropEffectMove

        Else

            Effect = vbDropEffectCopy

        End If

    End If

End Sub

 

'The BeginDrag event occurs when records are being dragged from\within the Report Control, and provides a

'ReportRecords collection containing all of the selected records when the drag operation started.

Private Sub wndReport_BeginDrag(ByVal Records As XtremeReportControl.IReportRecords)

    Debug.Print "Begin Drag. Records.Count = " & Records.Count

End Sub

 

'The DropRecords event occurs when records are dropped into a Report Control, and provides a

'ReportRecords collection containing all of the records that will be dropped into the Report Control.

'This gives the opportunity to modify the ReportRecords collection before they are actually added

'to the Report Control.

Private Sub wndReport_DropRecords(ByVal Records As XtremeReportControl.IReportRecords)

    Debug.Print "Drop Records. Records.Count = " & Records.Count

End Sub