I have store the instance of my class in clipboard and when retrieve the same from clipboard then getting null always. i marked my class [Serializable] but still no luck. i am not being able to find out where i made the mistake. Here is my code. please have a look and tell me where i made the mistake and what to change in code as a result i should be able to retrieve the object i stored in clipboard.
//Save object to clipboard
private void BtnSet_Click(object sender, EventArgs e)
{
NodeCollection ndCollection = new NodeCollection();
TunerDetails tds = null;
selectednode = tvCsmTuner.SelectedNode;
if (selectednode == null) return;
if (selectednode != null)
{
ndCollection.Name = selectednode.Text;
ndCollection.Text = selectednode.Text;
ndCollection.Tag = selectednode.Tag;
foreach (TreeNode node in selectednode.Nodes)
{
ndCollection.Nodes.Add(new NodeCollection { Name = node.Text, Text = node.Text, Tag = node.Tag });
}
DataObject data = new DataObject();
data.SetData(ndCollection);
Clipboard.SetDataObject(data);
}
RemoveNode();
}
//Read object back from clipboard
private void BtnGet_Click(object sender, EventArgs e)
{
DataObject retrievedData = (DataObject)Clipboard.GetDataObject();
if (retrievedData.GetDataPresent(typeof(NodeCollection)))
{
NodeCollection ndCollection1 = retrievedData.GetData(typeof(NodeCollection)) as NodeCollection;
if (ndCollection1 != null)
{
MessageBox.Show(ndCollection1.Name);
}
}
}
[Serializable]
public class NodeCollection
{
public NodeCollection() {
Nodes = new List<NodeCollection>();
}
#region PROPERTIES
public string Name { get; set; }
public string Text { get; set; }
public object Tag { get; set; }
public List<NodeCollection> Nodes { get; set; }
#endregion
}
Below code is working fine in separate winform project but when i copy the same code in my real project then this line return NULL
return retrievedData.GetData(typeof(NodeCollection)) as NodeCollection;
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var expected = new NodeCollection();
expected.Name = "Root";
expected.Text = "Root";
expected.Tag = new object();
for (int x = 0; x <= 1; x++)
{
expected.Nodes.Add(new NodeCollection() { Name = "Child 1", Text = "Child 1", Tag = new object() });
}
SetClipboardData(expected);
}
private void button2_Click(object sender, EventArgs e)
{
var actual = GetClipboardData();
}
private void SetClipboardData(NodeCollection nodeCollection)
{
var data = new DataObject();
data.SetData(nodeCollection);
Clipboard.SetDataObject(data);
}
//Read object back from clipboard
private NodeCollection GetClipboardData()
{
NodeCollection nd = null;
var retrievedData = Clipboard.GetDataObject();
if (retrievedData.GetDataPresent(typeof(NodeCollection)))
nd= retrievedData.GetData(typeof(NodeCollection)) as NodeCollection;
return nd;
}
}
[Serializable]
public class NodeCollection
{
public NodeCollection()
{
Nodes = new List<NodeCollection>();
}
#region PROPERTIES
public string Name { get; set; }
public string Text { get; set; }
public object Tag { get; set; }
public List<NodeCollection> Nodes { get; set; }
#endregion
}
please help me what i am missing. thanks