DragItem's AddItem Drag Rectangle

Hello. Can someone explain what’s going on with my drag rectangle in this test project?

I’m trying to get a multi-selection listbox to align all drag rectangles, but it always seems the rectangle created from the row where the drag begins gets out of position.

The test project was originally created by @Greg O’Lone used by me as a template. The code in CustomListbox1.DragRow is the code I replaced.

Thank you.

I don’t know if there’s a proper way to do this but here’s a work around

The missing information is the mouse displacement from the initial MouseDown to when DragRow gets triggered. So store the global mouse coordinates in MouseDown

[code]Properties waMouseX and waMouseY As Integer

Function MouseDown(x As Integer, y As Integer) As Boolean
waMouseX = System.MouseX
waMouseY = System.MouseY
return false
End Function[/code]

Then In DragRow you can calculate the offset and add that to the items you add. You also need to account for whether the Listbox’s Border is showing as that insets the rectangle by 1 pixel left and right.

[code]Function DragRow(drag As DragItem, row As Integer) As Boolean

dim offX As integer = System.MouseX - waMouseX
dim offY As integer = System.MouseY - waMouseY

Dim i, lbHeaderHeight as Integer

lbHeaderHeight = 0

For i = 0 to Me.ListCount-1

If Me.Selected(i) then
  
  if (row <> i) then
    
    if Me.Border then
      Drag.AddItem(me.Left+offX+1, me.Top+lbHeaderHeight+(me.rowheight*i)+offY, me.width-2, me.rowheight)
    else
      Drag.AddItem(me.Left+offX, me.Top+lbHeaderHeight+(me.rowheight*i)+offY, me.width, me.rowheight)
    end
    
  else
    
    //Drag.AddItem(0,0,0,0)  //is this really necessary?
    
  end if
  
End if

Next

Return True // Allow the drag

End Function[/code]

Here’s another workaround: don’t use the supplied DragItem. Instead create your own DragItem, drag that and return false so the drag parameter is ignored.

[code]Function DragRow(drag As DragItem, row As Integer) As Boolean
#pragma unused drag
#pragma unused row

//collect rows
dim rowsToDrag() As integer
for i As integer = 0 to ListCount - 1
if Selected(i) then rowsToDrag.Append i
next

if rowsToDrag.Ubound < 0 then break //error, can this ever happen?

//create new DragItem with first row
dim myDrag As new DragItem(self, 0, RowHeight*rowsToDrag(0), Width, RowHeight)

//add in subsequent rows
for i As integer = 1 to rowsToDrag.Ubound
myDrag.AddItem(0, RowHeight*rowsToDrag(i), Width, RowHeight)
next

//do drag with myDrag
myDrag.Drag

//don’t drag the drag parameter
return false

End Function[/code]

Also note both of these workarounds aren’t accounting for scroll. If the Listbox can be scrolled then subtract RowHeight*ScrollPosition from the rectangles Y coordinate.