Pixytech

Lead Architect  •  Full Stack Engineer

Silverlight – Implementing Clipboard support

Written by

in

,

Silverlight 4 (currently in beta) adds support for Clipboard! – However no option to access html in clipboard.
In Silverlight 3 we had copy/paste available in the Textbox, programmatic access could be done, some IE only solution exists via HTML DOM bridge (so not OOB) or cross-browser (involving Flash!). But now the game has changed as we now have an API for multi-platform Clipboard access.
In this beta, the support is for (Unicode) text-only, and the Clipboard class has 3 static methods:
• GetText()
• SetText()
• ContainsText()
Clipboard access can only be done from a user initiated action (mouse, keyboard), and user is prompted to acknowledge the first time Clipboard is set or read (once per session).
Silverlight 3 Clipboard Class


Below is the class for Clipboard support in silverlight 3 or 4 with an additional method GetHtmlData() to return formated text from clipboard.
Class Name : Clipboard.cs, Download Source Code.

public static class Clipboard
{
public static void Copy(KeyEventArgs e, string s);
private static string GetData(string type);
public static string GetHtmlData();
public static string GetTextData();
public static void SetData(string data);
public static bool IsEnabled;
}

Same tech. can be used in Silverlight 4 as this class provides methods like GetHtmlData() which can be used to get Html contents.This could be very helpfull in RichTextBox Editor,i.e if you could write parser for office html (mso) you may write code for copy/paste of contents from office applications to silverlight application.Example office excel cells will be treated as HTML table cells.Copying images from word documents will provide path to imagedata (VML) which can be then uploaded using OpenFileDialog calls..

Regards
Rajneesh Noonia

8 responses to “Silverlight – Implementing Clipboard support”

  1. hhh avatar
    hhh

    (ScriptObject)_jsPaste.InvokeSelf(new object[0]);
    or
    _jsCopy.InvokeSelf(new object[] { data });
     
    always throws null i dono why

    1. Rajneesh Noonia avatar

      Check if javascript is enabled or not.. , try running application in another browser.

  2. Ony avatar
    Ony

    This works on my local PC, however, upon deploying it on a dev server, it threw an exception.  I’m using GetHtmlData to get data from an excel worksheet.  Hope you can help me with this 🙂  Thanks!

  3. Rajneesh Noonia avatar

    When you try to access the clipboard,browser should prompt to allow access prompt & if user confirms it should be able to access the clipboard data.Try following RTF editor link
    http://www.rajneeshnoonia.com/blog/uploads/code/RTF/RichTextBoxTest.html

    Launch RTF editor from above link and open excel,Add some contents to few cells and copy the cells from excel and paste it into RTF editor control.It works for me on IE and chrome.

  4. Ony avatar
    Ony

    The thing is, it’s not even asking me for anything.  IE8 just closes and recovers the tab.  In Firefox, at least exception is being caught, but IE8 just closes.  I also tried to run IE as administrator but found no luck.  Any other suggestions?

    1. Rajneesh Noonia avatar

      Check the IE settings > tools > internet options> security > custom level (internet)> Allow Programmatic clipboard access – should be Prompt or enabled

  5. A avatar
    A

    Hi Rajneesh,
    I saw the demo at http://www.rajneeshnoonia.com/blog/uploads/code/RTF/RichTextBoxTest.html
    Its really good one. Can u pls share the clipboard parser code used in demo.
     

Leave a Reply

All posts

Discover more from Pixytech

Subscribe now to keep reading and get access to the full archive.

Continue reading