Here is the code that I use to carry out a drag & drop on my TreeView, it goes very well !
but when I use my own custom TreeNode (named : 'UsrTreeNode' and have some properties added) instead of the original 'System.Windows.Forms.TreeNode' , the drag & drop do not have any effect.
I tried doing something like :
1) My own custom TreeNode.
<Code>
Public Class UsrTreeNode
Inherits System.Windows.Forms.TreeNode
Private NodeType As String
Public Sub New(ByVal [text] As [String], ByVal [Id] As [Double], ByVal [ImgIndx] As Integer, ByVal [SelImgIndx] As Integer)
MyBase.New([text],[ImgIndx], [SelImgIndx])
MyBase.Tag = [Id]
End Sub
Public Property Type()
Get
Return NodeType
End Get
Set(ByVal Value)
NodeType = Value
End Set
End Property
End Class
</Code>
2) My DragDrop event :
If I populate my TreeView with my custom UsrTreeNode instead of original TreeNode and use the same code (by replacing Treenode by UsrTreeNode) my drag and drop doesn't work and no error raised !!
<Code>
Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop
Dim NewNode As UsrTreeNode
'----If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then
If e.Data.GetDataPresent("MyApp.UsrTreeNode", False) Then
Dim pt As Point
Dim DestinationNode As UsrTreeNode
pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
'NewNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode"), UsrTreeNode)
'DestinationNode.Nodes.Add(NewNode.Clone)
DestinationNode.Expand()
'Remove original node
NewNode.Remove()
End If
End Sub
</Code>
Any idea.
Thanks

The Treeview Drag&Drop AND using custom TreeNode !
venkateshwar rao gundlapally
Bryan St. Amour
(I use : DestinationNode.Nodes.Add(NewNode.Clone) to add my NewNode CLONE to my TreeView)
ngc
Trevor Hancock MSFT
but here is what happens :
before grag&drop :
ParentNode
|_______ Child_1
|_______ Child_2
|_______ Child_3
After Drag&Drop to a DestinationNode i obtain :
DestinationNode
......|
......|__ Parent node
.................|_______ Child_1
.................|_______ Child_2
.................|_______ Child_3
.................|_______ Child_1
.................|_______ Child_2
.................|_______ Child_3
Instead of :
DestinationNode
......|
......|__ Parent node
.................|_______ Child_1
.................|_______ Child_2
.................|_______ Child_3
how I can remove doubled nodes
Oyster Poh
FrdBed
NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode", True), UsrTreeNode)
Of course, it's also useful to make sure that your Type string is fully qualified and you don't have a phantom namespace somewhere.
porov
Mark Phillips
Public Overrides Function Clone() As Object
Dim CloneNode As YourNode = New YourNode()
'copying my properties
CloneNode.Prop1 = Prop1
CloneNode.Prop2 = Prop2
Return CloneNode
End Function
Andr&#233; Alves
Here is my UsrTreeNode Class :
<Code>
Public Class UsrTreeNode
Inherits System.Windows.Forms.TreeNode
Private NodeType As String
Public Property Type()
Get
Return NodeType
End Get
Set(ByVal Value)
NodeType = Value
End Set
End Property
End Class
</Code>
and here is my Drag&Drop sub :
<Code>
Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop
Dim NewNode As UsrTreeNode
If e.Data.GetDataPresent("GPI.UsrTreeNode", False) Then
Dim pt As Point
Dim DestinationNode As UsrTreeNode
pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
NewNode = CType(e.Data.GetData("GPI.UsrTreeNode",True), UsrTreeNode)
DestinationNode.Nodes.Add(NewNode.Clone)
DestinationNode.Expand()
'Remove original node
NewNode.Remove()
End If
End Sub
</Code>
Any Idea please
mjt
the code that I use is as follows:
<Code>
Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop
Dim NewNode As UsrTreeNode
If e.Data.GetDataPresent("MyApp.UsrTreeNode", False) Then
Dim pt As Point
Dim DestinationNode As UsrTreeNode
pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode"), UsrTreeNode)
DestinationNode.Nodes.Add(NewNode.Clone)
DestinationNode.Expand()
'Remove original node
NewNode.Remove()
End If
End Sub
</Code>
Thanks.
SMILEMan
Public Overrides Function Clone() As Object
Dim CloneNode As YourNode = New YourNode()
Dim TempNode As YourNode
If Nodes.Count > 0 Then
For Each TempNode In Nodes
CloneNode.Nodes.Add(CType(TempNode.Clone, YourNode))
Next
End If
'copying my properties
CloneNode.Prop1 = Prop1
CloneNode.Prop2 = Prop2
Return CloneNode
End Function