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