SharpClipboard icon indicating copy to clipboard operation
SharpClipboard copied to clipboard

Clipboard Image detected for twice

Open guzelonur opened this issue 2 years ago • 2 comments

Hi,

I think there might be some bug with the library itself, or the problem might be underlying on System.Windows.Forms.Clipboard class, when I set an image using System.Windows.Forms.Clipboard.SetImage() method, SharpClipboard's ClipboardChanged event is fired TWICE for no reason.

Is there anyone noticed that problem which occurs when you copy/set any image using Clipboard.SetImage() method?

Best regards.

guzelonur avatar Mar 10 '22 21:03 guzelonur

Hi everyone, I handled this issue with set and check last object that I get from clipboard and if they were equal I ignore second object!

Mehrdad32 avatar Mar 11 '22 15:03 Mehrdad32

The Clipboard.SetImage() method changes the clipboard twice: 1 - It clears the clipboard 2 - It loads the new image into the clipboard.

This is also true of Clipboard.SetText() and others ...

The way I handle this is to create a global class property (integer) called "Ignore". Every time I use the various .Set methods, I first increment (increase) the "Ignore" property by 2. When the WM message comes in (or when SharpClipboard's "ClipboardChanged" event is fired), I have an IF statement, so that it will only process that clipboard message if "ignore" = 0. Each time we ignore a message, we decrement (decrease) the "Ignore" property by 1 after we ignore the message. This will allow us to know when an end-user has copied something new to the clipboard, vs. when we have changed the clipboard ourselves with our code.

Best of luck to all in their coding.

MontanaCode avatar Apr 02 '22 23:04 MontanaCode