Clipboard don't work as expected for custom format - more clarifications

  • Thread starter Cristian Balcanu
  • Start date
C

Cristian Balcanu

I want to put a custom object in clipboard and later to be able to extract
it. I had red the Charles Petzold book "Programming Microsoft Windows with
C#" at chapter Chapter 24. In Petzold book I have read that is possible to
put a
reference to a custom type in clipboard and later you are able to read it.
As an example was shown how to put a System.Windows.Forms.Button class in
clipboard. I tried this but it doesn't worked. For details see code bellow.


using System;
using System.Windows.Forms;

namespace TestClipboard
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.ApartmentState =
System.Threading.ApartmentState.STA;

Button btn = new Button();
btn.Text = "dummy text";
Clipboard.SetDataObject(btn);

IDataObject dataObject = Clipboard.GetData() as IDataObject;
if(dataObject != null && dataObject.GetDataPresent(typeof(Button)))
{
object btnFromClipboardAsObj =
dataObject.GetData(typeof(Button).ToString(), true);
// here the extracted object is null
Button btnFromClipboard = btnFromClipboardAsObj as Button;

if(btnFromClipboardAsObj != null)
{
// this is the path that program should use.
// Unfortuantely is not using this.
System.Diagnostics.Trace.WriteLine("btnFromClipboardAsObj != null");
}
else
{
// this is the path code that program is following.
System.Diagnostics.Trace.WriteLine("btnFromClipboardAsObj == null");
}

}
}
}
}

Please let me know if I'm wrong and where I'm wrong.
I used visual studio 7.1 with .NET Framework 1.1.4322 SP1

Same source-code work as I expected on .NET 2.0.4115
that comes with vs2005 December CTP

If this is a .NET Framework 1.1 bug where shoud I report it
and what is the workaround?


Cristi
 
R

Roger

At first glance, it looks like you're pulling the data incorrectly from
the IDataObject:

dataObject.GetData(typeof(Button).ToString(), true);

I don't think the "ToString()" call should be in there. You should be
able to just use the object's type.

As an aside, I don't think it's a good idea to put a Button object on
the clipboard. Since Buttons (and other controls) are associated with
a particular window handle, and therefore a thread, it's probably not a
good idea to stick them on the clipboard, where they could be read by
any application.
 
C

Cristian Balcanu

Hi Roger

The Button case was only for demonstration and I chose it because is given
as example in Petzold book.
However because I'm getting the data from clipboard in the same AppDomain
with the thread that put it is should work for whatever object I put in
Clipboard.
I tested the same code by replacing the Button with the DataRow, that have a
[Serializable] attribute.

See below the code used for tests.
using System;

using System.Windows.Forms;

namespace TestClipboard
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.ApartmentState =
System.Threading.ApartmentState.STA;

System.Data.DataTable dt = new System.Data.DataTable("Test");
System.Data.DataRow dr = dt.NewRow();
Clipboard.SetDataObject(dr);

IDataObject dataObject = Clipboard.GetDataObject() as IDataObject;
if(dataObject != null &&
dataObject.GetDataPresent(typeof(System.Data.DataRow)))
{
object fromClipboardAsObj =
dataObject.GetData(typeof(System.Data.DataRow));
// here the extracted object is null

if(fromClipboardAsObj != null)
{
// this is the path that program should use.
// Unfortuantely is not using this.
System.Diagnostics.Trace.WriteLine("fromClipboardAsObj != null");
}
else
{
// this is the path code that program is following.
System.Diagnostics.Trace.WriteLine("fromClipboardAsObj == null");
}

}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top