Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Copy Text to Clipboard in Windows Phone 8

DZone's Guide to

How to Copy Text to Clipboard in Windows Phone 8

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

Windows Phone by default has a builtin mechanism to support copy and paste functionality, but in some case we may need to handle the copy experience explicitly.

We won't be able to paste outside a TextBox, since this is handled by the Operating System functionality. So, there is no need to handle this in code.

The Windows Phone “System.Windows” namespace exposes the Clipboard class having the following three methods to handle the Clipboard. They are:

  • ContainsText()
  • GetText()
  • SetText(string)

ContainsText(): The ContainsText() method queries the clipboard for the presence of data in the Unicode text format and returns a bool value of true or false.

Code snippet

GetText(): The GetText() method helps to get the copied text from the clipboard. This method returns the copied Unicode text data if text is present in the clipboard, otherwise it returns an empty string.

Code snippet

Clipboard.GetText();

SetText(string text): The SetText(string text) method sets Unicode text to store/copy on the clipboard.

Code snippet

Clipboard.SetText("text to copy");

Now, here is the demo for Windows Phone 8.

Create a new Windows Phone 8 project and create one button and two textboxes as shown in the following clipboard operation.


Figure 1: PageName

XAML Code

  1. <phone:PhoneApplicationPage
    x:Class="ClipBoardDemo.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
     <!--LayoutRoot is the root grid where all page content is placed-->
     <Gridx:Name="LayoutRoot"Background="Transparent">
     <Grid.RowDefinitions>
     <RowDefinitionHeight="Auto"/>
     <RowDefinitionHeight="*"/>
     </Grid.RowDefinitions>
     <!--TitlePanel contains the name of the application and page title-->
     <StackPanelx:Name="TitlePanel"Grid.Row="0"Margin="12,17,0,28">
     <TextBlockText="MY APPLICATION"Style="{StaticResource PhoneTextNormalStyle}"Margin="12,0"/>
     <TextBlockText="page name"Margin="9,-7,0,0"Style="{StaticResource PhoneTextTitle1Style}"/>
     </StackPanel>
     <!--ContentPanel - place additional content here-->
     <Gridx:Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0"></Grid>
     <ButtonContent="CopyToClipBoard"HorizontalAlignment="Left"Margin="107,229,0,0"Grid.Row="1"VerticalAlignment="Top"Width="260"Click="Button_Click_1"/>
     <TextBoxHorizontalAlignment="Left"Height="72"TextWrapping="Wrap"VerticalAlignment="Top"Width="456"Margin="0,321,0,0"Grid.Row="1"/>
     <TextBoxx:Name="clipboardTextbox"HorizontalAlignment="Left"Height="72"TextWrapping="Wrap"VerticalAlignment="Top"Width="456"Margin="2,152,0,0"Grid.Row="1"/>
     </Grid>
    </phone:PhoneApplicationPage>

Now, write the following code for the button click event.

C# Code

  1. privatevoid Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Clipboard.SetText(clipboardTextbox.Text.ToString()); 
    }

Next, run your application and enter some text into the clipboardTextbox as shown in the following screenshot.


Figure 2: Enter text in the ClipBoard TextBox

Click the CopyToClipBoard button to copy the text to the clipboard. Now, place the cursor onto another TextBox and you can see the paste symbol, press paste to paste the copied text as shown in the following screenshot.



Figure 3: Copy To ClipBoard

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
mobile ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}