0

I'm using Clipboard events to copy data from clipboard to my application which includes a Microsoft Word.

it will paste data to the word as soon as the user hits copy anywhere in windows. But I don't want to paste the same data if the user hits Ctrl + C twice. Now as this answer suggests, I can't use IsCurrent to compare DataObjects

So I'm doing it this way:

if (Clipboard.GetData(DataFormats.UnicodeText).ToString()!=oldData)
  {
     //Paste and stuffs
     oldData= Clipboard.GetData(DataFormats.UnicodeText).ToString();
  }

But it only works if the data contains some texts and I'm getting null reference error if it doesn't. So is there any way to know if the DataObject contains anthing else than text (say Bitmap) and be able to compare them?

Community
  • 1
  • 1
Ghasem
  • 14,455
  • 21
  • 138
  • 171
  • The only thing in this code that could cause a null pointer error is the `.ToString()` call. You could just fetch `Clipboard.GetData(DataFormats.UnicodeText)` into an Object variable in advance and check it for null, you know... that's kind of the standard way to avoid null pointers in general. – Nyerguds Oct 05 '17 at 07:25

1 Answers1

0

you can use method ContainsData to check the type of object exists or not

  if (Clipboard.ContainsData(System.Windows.Forms.DataFormats.Text))
        {
            //do something
        }

you can explore DataFormats class for formats suited to your needs

Akshita
  • 849
  • 8
  • 15