How do you cut, copy, and paste an extended Node tree?

I have a C # project that uses version 4.0.net framework and runs on VS 2010. I created a tree view filled with some extended tree nodes. I want the user to be able to copy, cut and paste these nodes into the clipboard via the context menu or keyboard shortcuts (and not just drag and drop).

The code works fine when copying, but when I try to paste these nodes, it causes this error: Unable to pass an object of type "System.IO.MemoryStream" to enter "Namespace Path.TreeNodeEx".

Here are my cut / copy / paste methods.

public void Copy() { Clipboard.SetData("Tree Node Ex", CurrentTreeNode.Clone()); } public void Paste() { CurrentTreeNode.Nodes.Add((TreeNodeEx)Clipboard.GetData("Tree Node Ex")); } 

I suspect that the problem is related to serialization, but I tried to implement the ISeralizable interface and the [Serializable] attribute to no avail.

Any suggestions?

+1
source share
4 answers

It turns out that it is tied to each extended tree node. I had a dictionary in which additional information was stored. Apparently, you cannot serialize dictionaries, so this interferes with the serialization of any of the tree nodes.

I implemented ISerializable for these extended tree nodes, and then converted the dictionary to two lists, after which I converted back to a dictionary in the deserialization constructor.

+1
source

Try the following:

 Clipboard.GetDataObject().GetData(typeof(TreeNodeEx)) 
0
source
 public void copy() { tempNode = TreeView.SelectedNode; } public void paste() { TreeView.SelectedNode.Nodes.Add(new TreeNode(tempNode.Text)); tNode = TreeView.SelectedNode.LastNode; for (int i = 0; i <= tempNode.Nodes.Count - 1; i++) { TreeNode NodeClone = (TreeNode)tempNode.Nodes[i].Clone(); tNode.Nodes.Add(NodeClone); } public void cut() { tempNode = TreeView.SelectedNode; TreeView.SelectedNode.Remove(); } 
0
source

Old stuff, but I spent a couple of hours running into the same problem, so here's what works:

 Imports System.Runtime.Serialization Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Me.TV.Nodes.Clear() Dim tNode As New TemplateNode() Me.TV.Nodes.Add(tNode) For i As Integer = 1 To 5 Dim newNode As New TemplateNode() tNode.Nodes.Add(newNode) tNode = newNode Next Me.TV.ExpandAll() End Sub Private Sub TV_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles TV.ItemDrag DoDragDrop(Me.TV.SelectedNode, DragDropEffects.Copy + DragDropEffects.Move + DragDropEffects.Scroll) End Sub Private Sub TV_DragEnter(sender As Object, e As DragEventArgs) Handles TV.DragEnter e.Effect = DragDropEffects.Move End Sub Private Sub TV_DragOver(sender As Object, e As DragEventArgs) Handles TV.DragOver e.Effect = DragDropEffects.Move If (e.KeyState And 8) = 8 Then e.Effect += DragDropEffects.Copy End If End Sub Private Sub TV_DragDrop(sender As Object, e As DragEventArgs) Handles TV.DragDrop Dim TemplateNode As TemplateNode = e.Data.GetData(GetType(TemplateNode)) Me.TV.Nodes.Add(TemplateNode.Clone()) End Sub Private Sub bCopy_Click(sender As Object, e As EventArgs) Handles bCopy.Click My.Computer.Clipboard.SetData("TemplateNode", Me.TV.SelectedNode) End Sub Private Sub bCut_Click(sender As Object, e As EventArgs) Handles bCut.Click My.Computer.Clipboard.SetData("TemplateNode", Me.TV.SelectedNode) Me.TV.SelectedNode.Remove() End Sub Private Sub bPaste_Click(sender As Object, e As EventArgs) Handles bPaste.Click Dim TemplateNode As TemplateNode = My.Computer.Clipboard.GetData("TemplateNode") If Me.TV.SelectedNode Is Nothing Then Me.TV.Nodes.Add(TemplateNode) Else Me.TV.SelectedNode.Nodes.Add(TemplateNode) End If End Sub End Class <Serializable()> _ Public Class TemplateNode Inherits TreeNode Public MyString As String = "MyStringValue" Public MyDateTime As DateTime = DateTime.Now Public MyBytes() As Byte Public Sub New() Dim buff(255) As Byte For i As Integer = 0 To buff.GetUpperBound(0) buff(i) = CByte(Rnd() * 100) Next Me.MyBytes = buff Me.Text = Guid.NewGuid().ToString() End Sub Protected Friend Sub New(info As SerializationInfo, context As StreamingContext) MyBase.New(info, context) End Sub End Class 
0
source

All Articles