Understanding DoDragDrop

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
Summary
DoDragDrop unexpectedly reset the cursor position. I have a solution but it feels like a kludge. What don't I understand? Is there a better way?

Detail
I am developing an editor with a RichTextBox. Logic contains essentially this code supporting Drag&Drop
Private Sub rtbReport_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbReport.MouseDown
MouseIsDown = True​
End Sub​
Private Sub rtbReport_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbReport.MouseMove
If MouseIsDown Then
[SNIP - locate edit element (LinkedListNode) at this position]
Dim RI As ReptItem = RINode.Value
If RI.Type = ReptItem.RIType.Field OrElse RI.Type = ReptItem.RIType.Text Then
DoDragDrop(Me.DragNode, DragDropEffects.Move)​
End If
MouseIsDown = False​
End If​
End Sub​

For typing I expect users will normally click where they want to start typing. This raises the MouseMove event, which executes the above code. This should have been no problem as DoDragDrop effectively does nothing if the cursor is not moved more than DragSize, but I found that unless I had a checkpoint within the MouseMove event the cursor position was being reset to the start of the RTB

I have "solved" the problem with code like this: -
Debug.Print("#2. rtbReport.SelectionStart:" & rtbReport.SelectionStart)
Dim SaveSelectionstart = rtbReport.SelectionStart
DoDragDrop(Me.DragNode, Effect)
Debug.Print("#3. rtbReport.SelectionStart:" & rtbReport.SelectionStart)
rtbReport.SelectionStart = SaveSelectionstart
Debug.Print("#4. rtbReport.SelectionStart:" & rtbReport.SelectionStart)
This produces the correct result, with debug output like this: -
#2. rtbReport.SelectionStart:447
#3. rtbReport.SelectionStart:0
#4. rtbReport.SelectionStart:447​

Although I could simply remove the diagnostics and move on, this solution smells: it doesn't seem right that DoDragDrop has this unexpected side effect, and I worry that there is something going on that I don't understand that could bite me later. Can anybody tell me if there is a proper way to solve this problem?
 
Back
Top