Problème avec Drag And Drop TreeWiew
Salut,
J'ai trouvé un exemple sur le forum MDSN sur le drag and drop avec un TreeWiew.
Ceci fonctionne très bien si ce n'est que si je drag and drop tous en bas du TreeWiew et bien j'ai
une erreur:
L'exception System.NullReferenceException n'a pas été gérée par le code utilisateur
HResult=-2147467261
Message=La référence d'objet n'est pas définie à une instance d'un objet.
Source=WindowsApplication1
StackTrace:
à WindowsApplication1.FenêtreMenuMaker.ContainsNode(TreeNode node1, TreeNode node2) dans C:\Users\Dayvid\AppData\Local\Temporary Projects\WindowsApplication1\Fenêtre Menu Maker.vb:ligne 80
à WindowsApplication1.FenêtreMenuMaker.treeView1_DragDrop(Object sender, DragEventArgs e) dans C:\Users\Dayvid\AppData\Local\Temporary Projects\WindowsApplication1\Fenêtre Menu Maker.vb:ligne 52
à System.Windows.Forms.Control.OnDragDrop(DragEventArgs drgevent)
à System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs drgEvent)
à System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object pDataObj, Int32 grfKeyState, POINTSTRUCT pt, Int32& pdwEffect)
InnerException:
Je suppose que c'est parce que je drag and drop sur rien ?
Voici le code, il vous faudra créer une fenêtre avec un TreeWiew, ajouter des éléments dans
celui-ci puis exécutez, drag and drop un item vers le bas ou ya rien et la magie -> erreur :'(
Coller ce code dans votre classe form.
Je vous remercie de votre aide par avance.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) Handles TreeView1.ItemDrag
' Move the dragged node when the left mouse button is used.
If e.Button = MouseButtons.Left Then
DoDragDrop(e.Item, DragDropEffects.Move)
' Copy the dragged node when the right mouse button is used.
ElseIf e.Button = MouseButtons.Right Then
DoDragDrop(e.Item, DragDropEffects.Copy)
End If
End Sub 'treeView1_ItemDrag
' Set the target drop effect to the effect
' specified in the ItemDrag event handler.
Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragEnter
e.Effect = e.AllowedEffect
End Sub 'treeView1_DragEnter
' Select the node under the mouse pointer to indicate the
' expected drop location.
Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver
' Retrieve the client coordinates of the mouse position.
Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
' Select the node at the mouse position.
TreeView1.SelectedNode = TreeView1.GetNodeAt(targetPoint)
End Sub 'treeView1_DragOver
Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragDrop
' Retrieve the client coordinates of the drop location.
Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
' Retrieve the node at the drop location.
Dim targetNode As TreeNode = TreeView1.GetNodeAt(targetPoint)
' Retrieve the node that was dragged.
Dim draggedNode As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
' Confirm that the node at the drop location is not
' the dragged node or a descendant of the dragged node.
If Not draggedNode.Equals(targetNode) AndAlso Not ContainsNode(draggedNode, targetNode) Then
' If it is a move operation, remove the node from its current
' location and add it to the node at the drop location.
If e.Effect = DragDropEffects.Move Then
draggedNode.Remove()
targetNode.Nodes.Add(draggedNode)
' If it is a copy operation, clone the dragged node
' and add it to the node at the drop location.
ElseIf e.Effect = DragDropEffects.Copy Then
targetNode.Nodes.Add(CType(draggedNode.Clone(), TreeNode))
End If
' Expand the node at the location
' to show the dropped node.
targetNode.Expand()
End If
End Sub 'treeView1_DragDrop
' Determine whether one node is a parent
' or ancestor of a second node.
Private Function ContainsNode(ByVal node1 As TreeNode, ByVal node2 As TreeNode) As Boolean
' Check the parent node of the second node.
If node2.Parent Is Nothing Then
Return False
End If
If node2.Parent.Equals(node1) Then
Return True
End If
' If the parent node is not null or equal to the first node,
' call the ContainsNode method recursively using the parent of
' the second node.
Return ContainsNode(node1, node2.Parent)
End Function 'ContainsNode |