Drag and Drop Multiple Rows Between ListBoxes

The text below comes from the documentation and it does not works as expected at all.

a. Function DragRow code below does not adds multiple Rows (only one at a time),

b. Once you selected two or more Rows from ListBox1 and drag ‘n’ Drop them, only one (or none) is added to ListBox2

c. I do not recall if this s is this example or the previous one (in the docs), but drag and drop into an open folder (Finder) create as many clippedText as selected Rows…

In fact, and after spending some hours on the subject, my deep feeling is that the whole explanation / examples does not works at all.

Tested with Xojo 2015r1 + Xojo 2015r2.2 / Maverick + Yosemite.

BTW: for the proof of concept, and easy to use, I created a brand new project with one window and put two ListBox (ListBox1 and ListBox2) in it. The idea is to drag and drop text data from one ListBox (Application 1) into another ListBox (Application 2) or eventually from ListBox1 in Window1 to ListBox2 in Window2.

Documentation quote:
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

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

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

Has anyone figured out how to do this?

I am thinking about a work-around that would store the selected rows in a string array then use DropObject to add them to the second listbox then delete them from the first one.

Hi Dean,

I do not what have changed since my original post (me ?), but it works now for the part --> Drag from Listbox1 to Listbox2.
(all Selected Rows from Listbox1 are copied into Listbox2).

If you drag more than one Row into the OS X Finder, you will get more than one clipping file… I do not had time to check on Windows 10.

To delete the MOVED * Rows from Listbox1, put the code below:

[code]Sub DropObject(obj As DragItem, action As Integer)
Dim nRows, i As Integer
Dim Sel_Count As Integer
Dim Removed_Cnt As Integer

// Get the number of selected Rows (in Listbox1)
nRows = LB_Left.Listcount
Sel_Count = LB_Left.SelCount

Do
If Obj.TextAvailable Then
Me.AddRow(Obj.Text)
End if
Loop Until Not obj.NextItem

// For Dean Davidge: delere Selected Rows from Listbox 1
For i=nRows DownTo 0
If LB_Left.Selected(i) then
LB_Left.RemoveRow(i)
Removed_Cnt = Removed_Cnt + 1
If Sel_Count = Removed_Cnt Then // Did I removed all selected Rows ?
Exit
End If
End if
Next
End Sub[/code]

NOTA: I set LB_Left as the name for Listbox1 (and LB_Right for Listbox2). This is for clarity purposes.

I add a If Block to check if all selected Rows have been removed and exit (or not), because it may speed up the process if the Listbox number of Rows is high.

  • I used this word to be sure that it is what you want to do. This is not a standard drag and drop.
    When I use drag and drop it is to make a copy of the data, not a cut and paste equivalent. Of course, this is just me.