Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving node on treeview causing app to stop responding 1

Status
Not open for further replies.

Kenny62

Programmer
Mar 3, 2004
54
GB
Hi Guys, if i've implemented dragdrop code on a treeview to re-arrange the nodes.

I validate the node that i'm attempting to move in the DragOver event.
In final phase of validation, we must ensure that the TreeNode under the cursor is not the TreeNode that is being dragged or a child of that TreeNode. This is important; if we attempt to drop a TreeNode onto itself or a ChildNode, then the node and its siblings would all just vanish.

All node move actions work as expected except where the target node is the node being dragged or child of the dragged node.
The application stops responding.
Any idea why - and how to fix this problem. (Problem possibly with do-loop)
Thanks for any help in advance.


Private Sub tvOptions_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvOptions.DragOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
True) = False Then Exit Sub

'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreeview As TreeView = CType(sender, TreeView)

'As the mouse moves over nodes, provide feedback to
'the user by highlighting the node that is the
'current drop target
Dim pt As Point = _
CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)

'See if the targetNode is currently selected,
'if so no need to validate again
If Not (selectedTreeview.SelectedNode Is targetNode) Then
'Select the node currently under the cursor
selectedTreeview.SelectedNode = targetNode

'Check that the selected node is not the dropNode and
'also that it is not a child of the dropNode and
'therefore an invalid target
Dim dropNode As TreeNode = _
CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
TreeNode)

'If targetNode Is dropNode Then
' e.Effect = DragDropEffects.None
' Exit Sub
'End If

Do Until targetNode Is Nothing
Debug.WriteLine("tvOptions_DragOver, Do-loop")
If targetNode Is dropNode Then
e.Effect = DragDropEffects.None
Exit Sub
End If
targetNode = targetNode.Parent
Loop
End If
'Currently selected node is a suitable target
e.Effect = DragDropEffects.Move

End Sub
 
Im very interested on how you work this out,
but dont have time to play with it today.

silly question, maybe, if you change

e.Effect = DragDropEffects.Move to
e.Effect = DragDropEffects.Copy

and then delete the source node, do you even need
to check if your dropping on top of the source node?


if it is to be it's up to me
 
Hello Infinitelo, i found this solution on the net:


I 've been having problems in the DragOver event - can you see what is wrong! It would be nice to get this working properly.

(I'm using VS.Net 2003 for development, the author of the solution was possibly using an earier version of VS.Net...)
 
sorry didnt get back, had a CRAZY week
guess the solution was to .add, then remove the original, that way you never lose a node.



if it is to be it's up to me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top