Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NETCF .NET
I have a custom type, FooNode, defined in my C# code. I want to add an instance of that custom type to the global System.Windows.Forms.Clipboard, and then retrieve it from the clipboard again. The add seems to work, but I am not able to retrieve the instance. Upon retrieval, several exceptions print to standard output like the following:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll
(... and 9 more just like above)
 
The result of the retrieval is a null reference. There is no crash or halt. The above exceptions are being dealt with internally; I am not able to catch them. The problem is not with the DataObject itself, because I can retrieve my FooNode from it. I just can't retreive my FooNode from the clipboard's DataObject.
 
I am able to add and then retreive other types of objects to the system clipboard, such as strings and System.Guid. Why can I not retrieve an object of my custom type?
 
Following is my test code. Call FooTest.Test() to run.
 
using System;
using System.Diagnostics;
using System.Windows.Forms;
 
public class FooNode
{
  private Guid m_Guid;
  private string m_Name = String.Empty;
 
  public FooNode( )
  {
    m_Guid = Guid.NewGuid();
    m_Name = "Foo";
  }
 
  public Guid Guid
  {
    get { return m_Guid; }
    set { m_Guid = value; }
  }
 
  public string Name
  {
    get { return m_Name; }
    set { m_Name = value; }
  }
}
 
public class FooTest
{
  // Entry point for test of using system clipboard.
  public static void Test( )
  {
    FooNode fooNode = new FooNode();
 
    // Add a FooNode to the system clipboard.
    DataObject dob = new DataObject( fooNode );
    dob.SetData( typeof( Guid ), fooNode.Guid );
    dob.SetData( DataFormats.StringFormat, fooNode.Guid.ToString() );
    Clipboard.SetDataObject( dob );
 
    // Retrieve the FooNode from the system clipboard.
    // *** Notice that the returned object is null. ***
    object raw = Clipboard.GetDataObject().GetData( typeof( FooNode ) );
 
    // Demonstrate what can and cannot be retrieved from the clipboard.
    Spam( Clipboard.GetDataObject(), new Type[] { typeof( FooNode ), typeof( Guid ) } );
  }
 
  public static void Spam( IDataObject dob, Type[] types )
  {
    Debug.WriteLine( dob );
    Debug.Indent();
 
    Debug.WriteLine( "Data formats:" );
    Debug.Indent();
    string[] formatNames = dob.GetFormats( true );
    foreach ( string name in formatNames )
    {
      Debug.WriteLine( name );
    }
    Debug.Unindent();
 
    // Test if I can access the data by format name.
    foreach ( string name in formatNames )
    {
      if ( dob.GetDataPresent( name ) )
      {
        Debug.WriteLine( String.Format( "Present as format=\"{0}\"", name ) );
        Debug.Indent();
        object raw = dob.GetData( name );
        Debug.WriteLine( String.Format( "raw={0}", raw != null ? raw : "null" ) );
        Debug.Unindent();
      }
    }
 
    // Test if I can access the data by type.
    if ( types != null )
    {
      foreach ( Type type in types )
      {
        if ( dob.GetDataPresent( type ) )
        {
          Debug.WriteLine( String.Format( "Present as type={0}", type ) );
          Debug.Indent();
          object raw = dob.GetData( type );
          Debug.WriteLine( String.Format( "raw={0}", raw != null ? raw : "null" ) );
          Debug.Unindent();
        }
      }
    }
 
    Debug.Unindent();
  }
}
 
modified on Monday, May 18, 2009 8:21 PM
Posted 18-May-09 14:07pm
Edited 21-Jan-11 4:40am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

I'm no expert in this matter, but I doubt your object makes it to the Clipboard. I have a hunch you need a [Serializable] for your FooNode class. Also you may want to add ",true" to Clipboard.SetDataObject() to make your object survive your app.

Smile | :)

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 220
1 OriginalGriff 145
2 _Amy 115
3 manoj kumar choubey 110
4 Sergey Alexandrovich Kryukov 105
0 OriginalGriff 7,320
1 Sergey Alexandrovich Kryukov 6,043
2 Maciej Los 3,749
3 Peter Leow 3,408
4 DamithSL 2,555


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 21 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100