Clipboard object

L

Lou

How do I get the data in the clipbaord fro a registered data type.
The code snippet below doesn't work? But the VB6 example does.
What am I doing wrong.


private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.

DataFormats.Format myFormat = DataFormats.GetFormat("myDataType");


// Create a new instance of the DataObject interface.

IDataObject data = Clipboard.GetDataObject();


lstItems.Items.Add (data.GetData(myFormat));

}



In VB6 this DOES work, this seems very simple.

Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, Button As
Integer, Shift As Integer, X As Single, Y As Single)
My_Format = RegisterClipboardFormat("myFormat")
Dim a() As Byte
a = Data.GetData(My_Format)

MsgBox a
End Sub
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Lou,

Drag&Drop doesn't use the clipboard. So if you check the clipboard, as you
do, you might find something there, but it probably won't be what you
expect. Instead of reading the clipboard use the Data property of the
DragEventArgs parameter. There is the IDataObject you have to use to get
your dragged data.
 
L

Lou

Can you give me an example?
That would be very helpful.
i am struggling with C# comming from VB.
 
L

Lou

This works with standard text but not with my registered format?
if i drag text from wordpad it works but not from my other app that drops
"CF_MOSCtrlData" ???

private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.

DataFormats.Format myFormat = DataFormats.GetFormat("CF_MOSCtrlData");

if (e.Data.GetDataPresent(typeof(System.String)))

{

Object item = (object)e.Data.GetData(typeof(System.String));

lstItems.Items.Add (item);

Debug.WriteLine ("Data preesent");

}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Lou,
It depends how your data have been put in the data object.

1. If you use one of the standard clipboard data formats (despite these are
clipboard data formats the actual data don't come from the clipboard)

lstItems.Items.Add (e.Data.GetData(DataFormats.Text)); for example

2. If the data have been given with private data format as a string
(SetData(string, object) overload)
you should use

For example for data format has been used the string "MyDataFormat"
lstItems.Items.Add (e.Data.GetData("MyDataFormat"));

3. For setting data has been used SetData(object) or SetData(Type, object)
overload. To retrieve the data:

For example data type is MyDataType and SetData(data) has been used
lstItems.Items.Add (e.Data.GetData(typeof(MyDataType)));

If different type has been used, say MyType, as DataFormat -
SetData(typeof(MyType), data)

lstItems.Items.Add (e.Data.GetData(typeof(MyType)));


But bare in mind that the data of expected format may not be in the data
object

So you have to check with
e.Data.GetDataPresent(...) or checking the returned object from GetData(...)
against *null* before using the data.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Lou,
Private clipboard formats are registered by their symbolic name (string).
You should know the name of the format (the string) because there is no way
to know the numeric value. Since it might vary.

In this case I belive it is enough to do

Say the clipboard format has been registered as "MyFormat"
private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.



if (e.Data.GetDataPresent(DataFormats.GetFormat("MyFormat"))

{

Object item = e.Data.GetData("MyFormat");

lstItems.Items.Add (item);

Debug.WriteLine ("Data preesent");

}

I haven't tested this so, if it doesn't work change the string "MyFormat"
with
DataFromats.GetFormat("MyFormat");

To save the calls to that method you can save the returned ID in a local
variable and use that variable instead.

int format = DataFormats.GetFormat("MyFormat");
use it like
e.Data.GetData(format);

HTH
B\rgds
100
 
L

Lou

It does not work.
I tried all the ones you mentioned
This really seems like it should be simple. the other app is just dropping
text with a registered clipboard
type. Why is this so difficult for me????

private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.

DataFormats.Format MyFormat = DataFormats.GetFormat("CF_MOSCtrlData");

if (e.Data.GetDataPresent("CF_MOSCtrlData"))

//if (e.Data.GetDataPresent(DataFormats.GetFormat("MyFormat")));

{

Object item = e.Data.GetData("MyFormat");

Object item = e.Data.GetData(DataFormats.GetFormat("MyFormat"));

lstItems.Items.Add (item);

//lstItems.Items.Add (e.Data.GetData("MyDataFormat"));

//Debug.WriteLine ("Data->" + (e.Data.GetData("MyDataFormat")));

//lstItems.Items.Add (e.Data.GetData(typeof(MyDataType)));

//lstItems.Items.Add (e.Data.GetData(typeof(MyType)));

}
 
L

Lou

If I add a watch to the "item" variable, I can see the data in the "buffer"
as bytes?
How can I get to that data? the line with lstItems.Items,Add does nothing?


Object item = e.Data.GetData("CF_MOSCtrlData");

lstItems.Items.Add (item.ToString());
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Lou do you know how the other application prepares the dataobject?
Is it .NET application? Are you sure CF_MOSCtrlData is the string used to
register the format.

Usually CF_XXXXX are integer constants used for the well-known clipboard
formats.
If you have VS.NET try to use IDataObject Viewer utility. In my computer it
is located in
C:\Program Files\Microsoft Visual Studio .NET
2003\Common7\Tools\Bin\dobjview.exe

Start dobjview.exe drag something from your source program and drop it over
the IDataObject Viewer window. It will show all data formats the data have.
You will see the *string* names for all private formats. that name you have
to use with DataFormats.GetFormat method.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
 
L

Lou

The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Yeah, it seems the _buffer is not a public variable. Anyway the are should
be public method, property or indexer to get the data. what the "Watch"
says about the type of "item" what is it?

--
B\rgds
100

Lou said:
The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
Stoitcho Goutsev (100) said:
Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
 
L

Lou

I believe it was Object
Stoitcho Goutsev (100) said:
Yeah, it seems the _buffer is not a public variable. Anyway the are should
be public method, property or indexer to get the data. what the "Watch"
says about the type of "item" what is it?

--
B\rgds
100

Lou said:
The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
Stoitcho Goutsev (100) said:
Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
--
B\rgds
100


If I add a watch to the "item" variable, I can see the data in the
"buffer"
as bytes?
How can I get to that data? the line with lstItems.Items,Add does nothing?


Object item = e.Data.GetData("CF_MOSCtrlData");

lstItems.Items.Add (item.ToString());



Lou,
Private clipboard formats are registered by their symbolic name
(string).
You should know the name of the format (the string) because there
is
no
way
to know the numeric value. Since it might vary.

In this case I belive it is enough to do

Say the clipboard format has been registered as "MyFormat"
private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.



if (e.Data.GetDataPresent(DataFormats.GetFormat("MyFormat"))

{

Object item = e.Data.GetData("MyFormat");

lstItems.Items.Add (item);

Debug.WriteLine ("Data preesent");

}

I haven't tested this so, if it doesn't work change the string
"MyFormat"
with
DataFromats.GetFormat("MyFormat");

To save the calls to that method you can save the returned ID in a local
variable and use that variable instead.

int format = DataFormats.GetFormat("MyFormat");
use it like
e.Data.GetData(format);

HTH
B\rgds
100
 
L

Lou

- item {System.IO.MemoryStream} System.Object


Stoitcho Goutsev (100) said:
Yeah, it seems the _buffer is not a public variable. Anyway the are should
be public method, property or indexer to get the data. what the "Watch"
says about the type of "item" what is it?

--
B\rgds
100

Lou said:
The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
Stoitcho Goutsev (100) said:
Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
--
B\rgds
100


If I add a watch to the "item" variable, I can see the data in the
"buffer"
as bytes?
How can I get to that data? the line with lstItems.Items,Add does nothing?


Object item = e.Data.GetData("CF_MOSCtrlData");

lstItems.Items.Add (item.ToString());



Lou,
Private clipboard formats are registered by their symbolic name
(string).
You should know the name of the format (the string) because there
is
no
way
to know the numeric value. Since it might vary.

In this case I belive it is enough to do

Say the clipboard format has been registered as "MyFormat"
private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.



if (e.Data.GetDataPresent(DataFormats.GetFormat("MyFormat"))

{

Object item = e.Data.GetData("MyFormat");

lstItems.Items.Add (item);

Debug.WriteLine ("Data preesent");

}

I haven't tested this so, if it doesn't work change the string
"MyFormat"
with
DataFromats.GetFormat("MyFormat");

To save the calls to that method you can save the returned ID in a local
variable and use that variable instead.

int format = DataFormats.GetFormat("MyFormat");
use it like
e.Data.GetData(format);

HTH
B\rgds
100
 
L

Lou

- item {System.IO.MemoryStream} System.Object
- [System.IO.MemoryStream] {System.IO.MemoryStream} System.IO.MemoryStream
+ System.IO.Stream {System.IO.MemoryStream} System.IO.Stream
+ _buffer {Length=0x712} byte[]
_capacity 0x712 int
_expandable false bool
_exposable false bool
_isOpen true bool
_length 0x712 int
_origin 0x0 int
_position 0x0 int
_writable true bool
CanRead true bool
CanSeek true bool
CanWrite true bool
Capacity 0x712 int
Length 0x712 long
MemStreamMaxLength 0x7fffffff int
Position 0x0 long

Stoitcho Goutsev (100) said:
Yeah, it seems the _buffer is not a public variable. Anyway the are should
be public method, property or indexer to get the data. what the "Watch"
says about the type of "item" what is it?

--
B\rgds
100

Lou said:
The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
Stoitcho Goutsev (100) said:
Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
--
B\rgds
100


If I add a watch to the "item" variable, I can see the data in the
"buffer"
as bytes?
How can I get to that data? the line with lstItems.Items,Add does nothing?


Object item = e.Data.GetData("CF_MOSCtrlData");

lstItems.Items.Add (item.ToString());



Lou,
Private clipboard formats are registered by their symbolic name
(string).
You should know the name of the format (the string) because there
is
no
way
to know the numeric value. Since it might vary.

In this case I belive it is enough to do

Say the clipboard format has been registered as "MyFormat"
private void lstItems_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

// Creates a new data format.



if (e.Data.GetDataPresent(DataFormats.GetFormat("MyFormat"))

{

Object item = e.Data.GetData("MyFormat");

lstItems.Items.Add (item);

Debug.WriteLine ("Data preesent");

}

I haven't tested this so, if it doesn't work change the string
"MyFormat"
with
DataFromats.GetFormat("MyFormat");

To save the calls to that method you can save the returned ID in a local
variable and use that variable instead.

int format = DataFormats.GetFormat("MyFormat");
use it like
e.Data.GetData(format);

HTH
B\rgds
100
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Yes, it can't be Object because Object class doesn't have _buffer member.
System.IO.MemoryStream that's it.
Read the stream the same way you would read a file. But what is the data
inside and what is the structure of the data you should know if you know
what CF_MOSCtrlData means.

To read the stream you can use the stream object directly or you can use one
of the reader classes
StreamReader, BinaryReader, etc

--
HTH
B\rgds
100

Lou said:
- item {System.IO.MemoryStream} System.Object
- [System.IO.MemoryStream] {System.IO.MemoryStream} System.IO.MemoryStream
+ System.IO.Stream {System.IO.MemoryStream} System.IO.Stream
+ _buffer {Length=0x712} byte[]
_capacity 0x712 int
_expandable false bool
_exposable false bool
_isOpen true bool
_length 0x712 int
_origin 0x0 int
_position 0x0 int
_writable true bool
CanRead true bool
CanSeek true bool
CanWrite true bool
Capacity 0x712 int
Length 0x712 long
MemStreamMaxLength 0x7fffffff int
Position 0x0 long

Stoitcho Goutsev (100) said:
Yeah, it seems the _buffer is not a public variable. Anyway the are should
be public method, property or indexer to get the data. what the "Watch"
says about the type of "item" what is it?

--
B\rgds
100

Lou said:
The other app is NOT a .Net app, its a standard windows app.
I can see the "item" object has a "buffer" section in the "Watch" window.
where I can see the data in bytes. Looks like unicode raw data.
I'm close but can't seem to get at the data?



- _buffer {Length=0xd8a} byte[]
[0x0] 0x3c byte
[0x1] 0x0 byte
[0x2] 0x6d byte
[0x3] 0x0 byte
[0x4] 0x6f byte
[0x5] 0x0 byte
[0x6] 0x73 byte
[0x7] 0x0 byte
[0x8] 0x3e byte
[0x9] 0x0 byte
[0xa] 0x3c byte
[0xb] 0x0 byte
[0xc] 0x6d byte
[0xd] 0x0 byte
[0xe] 0x6f byte
[0xf] 0x0 byte
[0x10] 0x73 byte
[0x11] 0x0 byte
[0x12] 0x49 byte
[0x13] 0x0 byte

and theres more
Lou,
In other words you do get data from the IDataObject, don't you?
What is the problem then?
--
B\rgds
100


If I add a watch to the "item" variable, I can see the data in the
"buffer"
as bytes?
How can I get to that data? the line with lstItems.Items,Add does
nothing?


Object item = e.Data.GetData("CF_MOSCtrlData");

lstItems.Items.Add (item.ToString());



Lou,
Private clipboard formats are registered by their symbolic name
(string).
You should know the name of the format (the string) because
there
 

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