Sharker Khaleed Mahmud Silverlight Tips & Tricks

September 15, 2010

88. Clipboard Support in Silverlight

Filed under: Silverlight — Tags: , — Sharker Khaleed Mahmud | shamrat231 @ 6:17 AM

[tweetmeme source=”shamrat231” only_single=false]

In this entry I am going to do a simple scenario involving copy and paste. Basically, the clipboard class has three members, ContainsText, GetText and SetText. To demonstrate, I will use a simple textbox.

You can see the live example here [live demo]   

The SOURCE CODE(.zip) is at the end of the page for download.

Create a simple xaml having a buttons and a textbox. Add a handler to each button so we can write code against the click events.

MainPage.xaml

    <toolkit:ExpressionDarkTheme>
    <StackPanel x:Name=”LayoutRoot”>
        <TextBox Text=”Copy paste sample” TextWrapping=”Wrap” Width=”400″ Height=”300″ />
        <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Center”>
            <Button Width=”120″ Height=”30″ Content=”Copy” Margin=”5″ Click=”Button_Click” />
            <Button Width=”120″ Height=”30″ Content=”Paste” Margin=”5″ Click=”Button_Click”/>
        </StackPanel>
    </StackPanel>
    </toolkit:ExpressionDarkTheme>

MainPage.xaml.cs

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Button)sender).Content.ToString())
            {
                case “Copy”:
                    {
                        // code goes here
                        break;
                    }
                case “Paste”:
                    {
                        // code goes here
                        break;
                    }
            }
        }

In the above code, I created a basic skeleton of the interface and sample code related to the button click event. Now lets take a paste scenario. Open a site and copy some text as shown in the below example.

Now, we want this text to be pasted in Silverlight so using clipboard support we can code like this

                case “Paste”:
                    {
                        if (Clipboard.ContainsText())
                        {
                            tbText.Text = Clipboard.GetText();
                        }
                        break;
                    }

Now run the app and click paste button.

Now lets handle the copy part.

                case “Copy”:
                    {
                        Clipboard.SetText(tbText.SelectedText);
                        break;
                    }

Now I will select part of the pasted text and copy

As always, you can download the source code from here [download link]

Sharker Khaleed Mahmud
Web Developer

Advertisements

5 Comments »

  1. It is very nice post.

    Comment by Chandra Dev — September 15, 2010 @ 12:04 PM

  2. […] Clipboard Support in Silverlight […]

    Pingback by Web Apps : : Silverlight Cream for September 15, 2010 — #948 - iPhone iPod iPad — September 16, 2010 @ 7:38 AM

  3. […] Clipboard Support in Silverlight (Sharker Khaleed Mahmud) […]

    Pingback by Windows Client Developer Roundup 042 for 9/20/2010 - Pete Brown's 10rem.net — September 20, 2010 @ 6:39 AM

  4. […] сайте SilverlighTips.net появились две любопытные заметки: использование буфера обмена (clipboard) и поддержка Drag-And-Drop в […]

    Pingback by Andrey on .NET | Интересности #6 — September 20, 2010 @ 9:28 AM

  5. Excellent blog! Do you have any helpful hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed .. Any ideas? Thanks a lot!

    Comment by Tesha Kloock — May 1, 2011 @ 4:25 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: