Clearing the Clipboard

  • Thread starter Thread starter jon morgan
  • Start date Start date
J

jon morgan

Hi,

How do I clear the system clipboard ? Used to be Clipboard.Clear in VB6 but
simply calling Clipboard.GetdataObject.GetData in VB.net doesn't seem to
actually remove the data.

Thanks

Jon
 
Hi Jon,
The clipboard is a shared resource. Hence, it is not correct to clear the
clipboard if you are not sure the the data in the clipboard had been put in
by the same application and the same instance of the application. This is
not easy to find out, though. In addition the clipboard shouldn't be used
for anything unless the user chooses so.
In other words if you put something in the clipboard it should stay there
until something else takes its place.

Anyways, the only way to clear the clipboard is to put something else on it.

I'd suggest the following:

Clipboard.SetDataObject(new DataObject());
 
Thanks Stoitcho,

I should have explained that I don't want to clear other applications'
clipboard data, just what my app. puts up there.

I know exactly what my app. puts on the Clipboard so why can't I erase it ?

Putting "dummy" data on with another call to SetDataObject to overwrite the
initial call seems a kludge at best and surely not very helpful to other
users of the shared resource.

Regards

Jon


Stoitcho Goutsev (100) said:
Hi Jon,
The clipboard is a shared resource. Hence, it is not correct to clear the
clipboard if you are not sure the the data in the clipboard had been put in
by the same application and the same instance of the application. This is
not easy to find out, though. In addition the clipboard shouldn't be used
for anything unless the user chooses so.
In other words if you put something in the clipboard it should stay there
until something else takes its place.

Anyways, the only way to clear the clipboard is to put something else on it.

I'd suggest the following:

Clipboard.SetDataObject(new DataObject());


--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

jon morgan said:
Hi,

How do I clear the system clipboard ? Used to be Clipboard.Clear in VB6 but
simply calling Clipboard.GetdataObject.GetData in VB.net doesn't seem to
actually remove the data.

Thanks

Jon
 
Hi Jon,

..NET uses the OleClipboard. This clipboard has no clear method. To clear the
clipboard one uses OleSetClipboard API passing NULL for the IDataObject.
..NET doesn't allow you to pass null to SetDataObject method. and as far as i
can see in the method's code it doesn't check whether the dataobject is
empty to pass NULL for you.

IMHO to set an empty object is not that bad idea as it seems you find it.
If you don't like it ofcourse you can use PInvoke and call OleSetClipboard
API from ole32.dll or you can do it in the old fashion way:
Use PInvoke and call the following sequence of methods declared in
user32.dll
OpenClipboard
EmptyClipboard
CloseClipboard


--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

jon morgan said:
Thanks Stoitcho,

I should have explained that I don't want to clear other applications'
clipboard data, just what my app. puts up there.

I know exactly what my app. puts on the Clipboard so why can't I erase it ?

Putting "dummy" data on with another call to SetDataObject to overwrite the
initial call seems a kludge at best and surely not very helpful to other
users of the shared resource.

Regards

Jon


Stoitcho Goutsev (100) said:
Hi Jon,
The clipboard is a shared resource. Hence, it is not correct to clear the
clipboard if you are not sure the the data in the clipboard had been put in
by the same application and the same instance of the application. This is
not easy to find out, though. In addition the clipboard shouldn't be used
for anything unless the user chooses so.
In other words if you put something in the clipboard it should stay there
until something else takes its place.

Anyways, the only way to clear the clipboard is to put something else on it.

I'd suggest the following:

Clipboard.SetDataObject(new DataObject());


--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

jon morgan said:
Hi,

How do I clear the system clipboard ? Used to be Clipboard.Clear in
VB6
but
simply calling Clipboard.GetdataObject.GetData in VB.net doesn't seem to
actually remove the data.

Thanks

Jon
 
Back
Top