DataObject Object

Description

A holding area for formatted text data used in transfer operations. Also holds a list of formats corresponding to the pieces of text stored in the DataObject.

Remarks

A DataObject can contain one piece of text for the Clipboard text format, and one piece of text for each additional text format, such as custom and user-defined formats.

A DataObject is distinct from the Clipboard. A DataObject supports commands that involve the Clipboard and drag-and-drop actions for text. When you start an operation involving the Clipboard (such as GetText) or a drag-and-drop operation, the data involved in that operation is moved to a DataObject.

The DataObject works like the Clipboard. If you copy a text string to a DataObject, the DataObject stores the text string. If you copy a second string of the same format to the DataObject, the DataObject discards the first text string and stores a copy of the second string. It stores one piece of text of a specified format and keeps the text from the most recent operation.

Methods

Clear method, GetFormat method, GetFromClipboard method, GetText method, PutInClipboard method, SetText method, StartDrag method.

See Also

BeforeDragOver event, BeforeDropOrPaste event.

Example

The following example demonstrates a drag-and-drop operation from one ListBox to another using a DataObject to contain the dragged text. This code sample uses the SetText and StartDrag methods in the MouseMove event to implement the drag-and-drop operation.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains two ListBox controls named ListBox1 and ListBox2. You also need to add choices to the second ListBox.

Private Sub ListBox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As _
        Single, ByVal DragState As Long, ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)
    Cancel = True
    Effect = 1
End Sub

Private Sub ListBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Action As Long, ByVal Data As MSForms.DataObject, ByVal X As  _
        Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal  _
        Shift As Integer)
    Cancel = True
    Effect = 1
    ListBox2.AddItem Data.GetText
End Sub

Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift _
        As Integer, ByVal X As Single, ByVal Y As Single)
    Dim MyDataObject As DataObject
    If Button = 1 Then
        Set MyDataObject = New DataObject
        Dim Effect As Integer
        MyDataObject.SetText ListBox1.Value
        Effect = MyDataObject.StartDrag
    End If
End Sub

Private Sub UserForm_Initialize()
    For i = 1 To 10
        ListBox1.AddItem "Choice " & (ListBox1.ListCount + 1)
    Next i
End Sub