Page 1 of 1

Silverlight 4 - Copy/Paste From Clipboard

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 28 October 2010 - 07:24 AM

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.

Attached Image

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...

Attached Image

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.

Attached Image

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.

Is This A Good Question/Topic? 2
  • +

Replies To: Silverlight 4 - Copy/Paste From Clipboard

#2 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

Posted 02 November 2010 - 08:07 PM

Bad-ass !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1