[RESOLVED] Tab control - Change cursor while dragging
Hi,
I've implemented Drag & Drop in Tab control and I want cursor to be changed while dragging Tabs. I got that part too, but with one slight problem - normally you change cursor on GiveFeedback event when you allready started drag drop action. Unfortunally I also set start of drag/drop in Mousedown, so when I click to change Tab page my new dragging cursor also appears for a moment, because left mouse click was done. Is there any way that I could start drag/drop with left mouse after I changed Tab page, and then change cursor ?
My code:
Code:
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
'Current tab is checked in MouseMove, and It's added into Tag
If Not Me.Tag Is Nothing Then
Dim clickedTab As TabPage = DirectCast(Me.Tag, TabPage)
Dim clicked_index As Integer = Me.TabPages.IndexOf(clickedTab)
' start drag n drop
Me.DoDragDrop(clickedTab, DragDropEffects.All)
End If
End Sub
Protected Overrides Sub OnGiveFeedback(gfbevent As GiveFeedbackEventArgs)
MyBase.OnGiveFeedback(gfbevent)
' Set the custom cursor based upon the effect.
gfbevent.UseDefaultCursors = False
If ((gfbevent.Effect And DragDropEffects.Move) = DragDropEffects.Move) Then
Cursor.Current = Cursors.Hand
Else
Cursor.Current = Cursors.Default
End If
End Sub
Re: Tab control - Change cursor while dragging
Generally you shouldn't start a "drag" operation on MouseDown because of problems like this. Every click starts a drag and drop operation, and that can cause weird things to happen.
The more correct way to start a drag and drop operation is to start it after the mouse is down *and* has moved by a certain margin. It turns out "a certain margin" is defined by SystemParameters.MinimumHorizontalDragDistance and SystemParameters.MinimumVerticalDragDistance. So you need to write something like this:
Code:
When MouseDown happens:
* Note the position of the cursor.
When MouseUp happens:
* If a drag/drop is in progress:
* Do the thing.
* Note a drag/drop can't be happening anymore.
When MouseMove happens:
* If the user is holding the button and a drag/drop isn't already in progress:
* If the mouse has moved far enough:
* Note a drag/drop is happening, update the cursor, and save any information you might need.
The documentation for DoDragDrop() has a pretty good implementation, though it looks like in theirs they drag things during MouseMove (which is sensible sometimes.)
Re: Tab control - Change cursor while dragging
Thanks Sitten,
very good example, didn't notice this solution at all. I've implemented drag operation in simmilar (but much simpler) way and It works good :)