Customize copied / pasted content depending on the target

I noticed that many applications are copied and pasted differently depending on the target application. For example, if I have an "HTML copy" element in TFS (in the web interface):

  • If I paste it into a notebook, I get a csv-like output
  • If I paste it into Excel, I get nicely formatted rows and columns
  • If I paste it into Outlook, I get a beautiful table

I tried to use several instances Clipboard.SetText(String, TextDataFormat)with different values TextDataFormat, but, alas, they overwrite each other (and nothing but TextDataFormat.Textseems to allow "pasting").

What I have tried so far:

private static void KeyPressed(KeyEventArgs e, GridView grid)
{
    if (e.Control && e.KeyCode == Keys.C)
    {
        var textContent = new StringBuilder();
        var htmlContent = new StringBuilder("<table>");

        // build content
        for (int i = 0; i < 10; i++)
        {
            htmlContent.AppendFormat("<tr><td>{0}</td></tr>", i);
            textContent.AppendFormat("{0}\t", i);
        }

        textContent.Length--; // remove last tab
        htmlContent.Append("</table>");

        // send contents to clipboard
        Clipboard.Clear();
        Clipboard.SetText(textContent.ToString(), TextDataFormat.CommaSeparatedValue);
        Clipboard.SetText(textContent.ToString(), TextDataFormat.Text);
        Clipboard.SetText(htmlContent.ToString(), TextDataFormat.Html);

        e.Handled = true;
        e.SuppressKeyPress = true;
    }
}
+4
1

, :

var dataObject = new DataObject(textContent); // allows data to auto-convert on paste
Clipboard.SetDataObject(dataObject);
0

All Articles