C#: When retrieve data from clipboard then getting NULL always

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
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. Thanks


//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
}

In same application when i try to save different object in clipboard this way then it worked. so now i more confused that customer data getting store and retrieve in clipboard properly but my nodecollection class is not getting retrieve from clipboard after save. what i am missing?

here is code which is working to save & retrieve customer data.

DataObject mydata = new DataObject();
Customer c = new Customer("Customer as Customer object");
c.Nodes.Add(new Customer { Name = "Hello" });
mydata.SetData(c);
Clipboard.SetDataObject(mydata);

DataObject MyretrievedData = (DataObject)Clipboard.GetDataObject();
if (MyretrievedData.GetDataPresent(typeof(Customer)))
{
Customer customer =
MyretrievedData.GetData(typeof(Customer)) as Customer;
if (customer != null)
{
MessageBox.Show(customer.Name);
}
}

[Serializable]
public class Customer
{
public Customer() { }
private string nameValue = string.Empty;
public Customer(String name)
{
nameValue = name;
Nodes = new List<Customer>();
}
public string Name
{
get { return nameValue; }
set { nameValue = value; }
}

public List<Customer> Nodes { get; set; }
}

please help me.


Thanks

Continue reading...
 
Back
Top