Derek Lakin

Creating Copyable Read-Only Text

Ignoring any debates or discussions about when it’s going to arrive if you haven’t got it already, but the recently-released update for Windows Phone 7 (WP7) code-named NoDo (apparently short for No Donuts) brought the much discussed copy and paste feature.

How Does it Work?

The copy and paste feature has been available in the developer tools since December 2010, so we already know that the copy and paste feature works in the following scenarios:

  • TextBox
  • PasswordBox
  • WebBrowser control, but only for TextBox elements on a web page, NOT all text.
  • Internet Explorer(IE), for any text not just TextBox elements.
IE vs. WebBrowser Control

I’m not going to go into why the WebBrowser control only supports this feature for HTML textboxes, but IE supports it for plain text as well (mainly because I don’t know), but if [Brandon Watson](http://twitter.com/BrandonWatson "Brandon Watson") or [Charlie Kindel](http://twitter.com/ckindel "Charlie Kindel") read this, I’d love to hear an explanation. For now we just have to deal with this fact and move on. Hopefully, in time this feature will be improved upon in a future release.

To copy a word simply tap on that word and it gets highlighted with the copy icon above it. You can extend the selection to more than one word by dragging the selection left or right accordingly. Once you’ve got the selection right, just tap the copy button. This results in the Paste icon being displayed at the top of the keyboard (SIP) at the bottom of the page. You can then tap any other supporting control to change focus, and then tap the Paste icon to paste the copied text. At this point, the Paste icon disappears, but don’t worry, if you want to paste the same text again somewhere else just wipe left to right where the Paste icon used to be and it will re-appear.

The copy and paste feature is also a global feature, so you can copy text in one application, open a new application, and then paste the copied text. This has particular advantage for applications that provide integration with web services that don’t support OAUTH or a similar convenient authentication mechanism and require an API key. You can navigate the user to the appropriate web page and instruct them to copy, and then paste their API key into your application.

## So, What’s the API for Copy and Paste? There is absolutely no API for the copy and paste feature (for now at least). I’m sure the stock answer is something to do with security, but we’ve got a clipboard API for “full fat” Silverlight and desktop applications, so I’m sure it will come in time. If you already have an application in the Marketplace that has TextBox, PasswordBox, or WebBrowser controls in it, then you do not need to do anything to that application for it to get copy and paste support. Equally, you don’t need to do anything to enable copy and paste in new applications either. The copy and paste feature is an operating system update that users get on their devices and does not require an update to your code. If, however, you have areas of your application that you want to be read-only, but you want the user to be able to copy them (such as an API key), then you can use a TextBox with the IsReadOnly property set to true to achieve this. ## The CopyTextBlock Style Although it’s simple enough to set IsReadOnly to true on a regular TextBox, this doesn’t provide a great user experience for the user because it still looks like a TextBox. If you want a TextBlock that the user can copy from, then you can use the following Style for a TextBox instead, which not only makes it look like a TextBlock, but also removes 75% of the visuals, so is more efficient.
code copy nodo passwordbox paste textbox styles webbrowser