1

I want go get value from 'd' IDataObject to 'c' IDataObject in C# but my 'c' possess value from my Clipboard even when i won't click D key. How can I give independent value from one IDataObject to second?

First function:

if (Keyboard.IsKeyDown(Key.D))
{
    d = Clipboard.GetDataObject();
    c = d;      // <-- doesn't work
    string dd = d.GetData(DataFormats.Text).ToString();      //content
    MessageBox.Show(dd);
}

Second function

if (Keyboard.IsKeyDown(Key.S))
{
    string dd = c.GetData(DataFormats.Text).ToString();      //content
    MessageBox.Show(dd);
}
  • Clipboard may contain Metadata that you can't copy to the object as it is. Btw, the question is not clear. What do you want to achieve? Where copy data from and where to? Why `Clipboard`? What the condition? Inside one application or different applications? Do you need Clipboard listening, for example when some other application modify it and you want to read the changes e.g. after virtual pressing `Ctrl+C`? `IDataObject` is reference to the data, not the data itself. – aepot May 22 '20 at 22:51
  • I gonna click CTRL + C. I have value in my `d`. When i click D key this shows me it using messege box and copy to `c`. Now I again click ctrl + c marking another text. After that when I click S key it should show my old value from first ctrl + c but i have content from my actual clipboard. – Dawid SoChii Sokół May 22 '20 at 23:06
  • I hope you know what i'm talking about bro. – Dawid SoChii Sokół May 22 '20 at 23:08
  • I want to get content(text or image) from one variable to different one but it should take it even if it's text or image. – Dawid SoChii Sokół May 22 '20 at 23:23

1 Answers1

2

Why do you need a backup of IDataObject?

If it was possible in all cases it would look like this

Backup

// expiremental list of formats to exclude. Doesn't cover all possible cases but most of it.
private static readonly string[] clipboardMetaFormats = { "application/x-moz-nativeimage", "FileContents", "EnhancedMetafile", "System.Drawing.Imaging.Metafile", "MetaFilePict", "Object Descriptor", "ObjectLink", "Link Source Descriptor", "Link Source", "Embed Source", "Hyperlink" };

private DataObject ReadClipboard()
{
    DataObject result = new DataObject();
    IDataObject dataObject = Clipboard.GetDataObject();
    string[] formats = dataObject.GetFormats()?.Except(clipboardMetaFormats).ToArray() ?? Array.Empty<string>();
    foreach (string format in formats)
    {
        try
        {
            object data = dataObject.GetData(format);
            if (data != null) result.SetData(format, data);
        }
        catch (ExternalException ex)
        { 
            Debug.WriteLine($"Error {ex.ErrorCode}: {ex.Message}");
        }
    }
    return result;
}
DataObject backup = ReadClipboard();

Then you may use it as local data storage. For example in case you want to change Clipboard, use the changed value e.g. paste it into some application and the restore the previous data to Clipboard.

Restore

private void UpdateClipboard(DataObject data)
{
    if (data == null) return;
    try
    {
        Clipboard.SetDataObject(data);
    }
    catch (ExternalException ex)
    {
        Debug.WriteLine($"Error {ex.ErrorCode}: {ex.Message}");
    }
}
UpdateClipboard(backup);

But in your case that's a simple string. You may do it this way:

string text = Clipboard.GetText(TextDataFormat.UnicodeText);
Clipboard.SetText(text, TextDataFormat.UnicodeText);
aepot
  • 4,558
  • 2
  • 12
  • 24
  • I need backup cuz I want to create multi clipboard function and when user will click ctrl+c program should save it to variable. When user click ctrl+c again old clipboard content should go to different variable and when i click one key my actual clipboard value should be replaced by old. I have this feature but only with texts. I need adjust it for texts and images(We have possibility to use printscreen button and it works like ctrl + c right) – Dawid SoChii Sokół May 22 '20 at 23:36
  • @DawidSoChiiSokół try the solution then. – aepot May 22 '20 at 23:39
  • I will try it but tomorrow and give you feedback. Thanks in advance. – Dawid SoChii Sokół May 22 '20 at 23:53
  • Your code works! Thanks a lot! I have only one question to you. From where do you have information about that? I want learn more about your code. Do you have any useful link to read about it? – Dawid SoChii Sokół May 23 '20 at 09:29
  • @DawidSoChiiSokół `IDataObject` is interface the object created by `Clipboard` and the object is being destroyed each time the clipboard changes. Also `Clipboard` may contain metadata that can't be backed up to the `object` instance, there should be more complicated solution to backup it. So, I ignore that things and that cause an `Exception` when you try to copy the entire `DataObject`. Thus I copy format by format ignoring everything I found that I can't copy. In a back way it works simply setting the entire `DataObject` to the `Clipboard`. I don't know where to read. – aepot May 23 '20 at 09:48
  • @DawidSoChiiSokół did I answered your question? Please mask the answer as accepted if I did. – aepot May 23 '20 at 19:32
  • I did it. What is it changing? I'm new on stackoverflow – Dawid SoChii Sokół May 24 '20 at 11:17
  • @DawidSoChiiSokół Thank you! It means that answer is correct and suits your needs and other members may not try to answer. It gives 15 reputation to the author of the answer and 2 reputation to you. – aepot May 24 '20 at 11:46