Winform: Always getting NULL when retrieve data from clipboard

Sudip Bhatt 2,271 Reputation points
2020-11-17T08:25:37.847+00:00

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-11-18T09:28:37.01+00:00

    Hi Sudip Bhatt,
    I checked the code examples you provided, the problem is in this line

    expected.Tag = new object();  
    

    As offical document said that "Any Object derived type can be assigned to tag property. If this property is being set through the Windows Forms designer, only text can be assigned".
    So you need to assign it with text instead of "new object()", which causes problem.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful