Silverlight 4 – Copy/Paste From Clipboard

In this tutorial, I will show how you can use Silverlight 4 to get access to the client clipboard. Silverlight 3 had limited clipboard access, but Microsoft implemented more complete access to the clipboard.

For security purposes, access to the clipboard is only allowed through a user-initialed event. For example, you couldn’t have a timer running in the background to constantly get text from the clipboard.

For first, let’s create a Silverlight 4 application and name it SL4ClipboardAccess. Then Click OK on the next popup.

10-28-2010 10-05-33 AM

I added some controls to the page..

<Grid x:Name="LayoutRoot" Background="White">

    <TextBox
        Name="CopyTextTextBox"
        Height="23"
        HorizontalAlignment="Left"
        Margin="132,36,0,0"
        VerticalAlignment="Top"
        Width="174" />

    <TextBlock
        Name="textBlock1"
        Text="Text To Copy:"
        Height="23"
        HorizontalAlignment="Left"
        Margin="47,40,0,0"
        VerticalAlignment="Top" />

    <Button
        Name="CopyToClipboardButton"
        Content="Copy To Clipboard"
        Click="CopyToClipboardButton_Click"
        Height="39"
        HorizontalAlignment="Left"
        Margin="144,81,0,0"
        VerticalAlignment="Top"
        Width="145"  />

    <Button
        Click="PasteToTextBoxButton_Click"
        Name="PasteToTextBoxButton"
        Content="Paste To Textbox"
        Height="39"
        HorizontalAlignment="Left"
        Margin="144,147,0,0"
        VerticalAlignment="Top"
        Width="145"  />

    <TextBox
        Name="PasteTextTextBox"
        Height="23"
        HorizontalAlignment="Left"
        Margin="132,209,0,0"
        VerticalAlignment="Top"
        Width="174" />

    <TextBlock
        Name="textBlock2"
        Text="Text From Clipboard:"
        Height="23"
        HorizontalAlignment="Left"
        Margin="8,209,0,0"
        VerticalAlignment="Top" />

</Grid>

Which look like this…

10-28-2010 10-19-03 AM

Now, the Click Event for the CopyToClipboard Button would look like this…

private void CopyToClipboardButton_Click(object sender, RoutedEventArgs e)
{
    string textToCopy = CopyTextTextBox.Text;

    try
    {
         Clipboard.SetText(textToCopy);
    }
    catch (SecurityException se)
    {
        MessageBox.Show(se.Message);
    }
}

and the Click Event for the PasteToTextBox Button would be this..

private void PasteToTextBoxButton_Click(object sender, RoutedEventArgs e)
{
    string textFromClipboard = string.Empty;

    try
    {
         textFromClipboard = Clipboard.GetText();
    }
    catch (SecurityException se)
    {
         MessageBox.Show(se.Message);
    }

    PasteTextTextBox.Text = textFromClipboard;
}

Now run the application and type into the Text To Copy textbox. When you click the Copy To Clipboard button, you will be prompted for permission for access to the clipboard.

10-28-2010 10-15-52 AM

After allowing access, click the Paste To TextBox button, and the text will now be in the other textbox.

Very simple tutorial for a very powerful feature.


1 comment

  1. Joann Gilbert February 5, 2012 8:29 pm  Reply

    Thanks a lot for providing individuals with an exceptionally splendid possiblity to read in detail from this website. It is often very good and also packed with amusement for me and my office acquaintances to search your blog a minimum of three times in 7 days to find out the latest stuff you have got. Not to mention, I’m also usually amazed concerning the mind-boggling solutions you serve. Some two ideas in this posting are easily the best we have had.

Leave a comment

Your email address will not be published. Required fields are marked *