ListBox.EnableDrag

From Xojo Documentation

Revision as of 23:00, 17 June 2019 by Gperlman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Property (As Boolean )
aListBox.EnableDrag = newBooleanValue
or
BooleanValue = aListBox.EnableDrag

Supported for all project types and targets.

Allows rows to be dragged.

Examples

Drag and Drop between ListBoxes

The following example allows the user to drag one row from ListBox1 to ListBox2. ListBox1 has its EnableDrag property set to True and its SelectionType property set to zero (Single). Its DragRow event handler is as follows:

Function DragRow(drag As DragItem, row As Integer) As Boolean
drag.Text = Me.List(row)
Return True // allow the drag

ListBox2's Open event handler has the line:

Me.AcceptTextDrop

Its DropObject event handler is this:

Sub DropObject(obj As DragItem)
Me.AddRow(obj.Text) // adds the dropped text as a new row

Drag and Drop Multiple Rows between ListBoxes

The following example allows the user to drag more than one row from ListBox1 to ListBox2. The dragged rows are added to the end of the list.

ListBox1 has its EnableDrag property set to True, enabling items in its list to be dragged, and its SelectionType property set to 1 (Multiple row selection). Its DragRow event handler is as follows:

Function DragRow (drag As DragItem, row As Integer) As Boolean
Dim nRows, i As Integer
nRows = Me.ListCount - 1
For i = 0 To nRows
If Me.Selected(i) Then
drag.AddItem(0, 0, 20, 4)
drag.Text = Me.List(i) // get text
End If
Next
Return True // allow the drag
End Function

It uses the AddItem method of the DragItem to add an additional item to the DragItem each selected row. The DropObject event handler then cycles through all items to retrieve all dragged rows.

ListBox2 has the following line of code in its Open event handler. It permits it to receive dragged text.

Me.AcceptTextDrop

Its DropObject event handler checks to see if the dragged object is text; if it is, it adds a row to the end of the list and assigns the text property of the dragged object to the new row: It loops through all items in the DragItem until NextItem returns False.

Sub DropObject(obj As DragItem)
Do
If obj.TextAvailable Then
Me.AddRow(obj.Text)
End If
Loop Until Not obj.NextItem
End Sub

You can also drag from ListBox1 to the desktop to get a text clipping or to another application that supports text drag and drop.