<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>David's Coding Adventures - A resource of technical tidbits</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/codingadventures/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/codingadventures/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:40:10 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Copying data to the clipboard in Silverlight</title>
      <link>https://docs.microsoft.com/archive/blogs/codingadventures/copying-data-to-the-clipboard-in-silverlight</link>
      <pubDate>Tue, 26 May 2009 18:26:00 GMT</pubDate>
      <dc:creator><![CDATA[davidyost]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/codingadventures/2009/05/26/copying-data-to-the-clipboard-in-silverlight/</guid>
      <description><![CDATA[I was recently trying to add the ability for our network operations team to be able to copy alert...]]></description>
      <content:encoded><![CDATA[<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">I was recently trying to add the ability for our network operations team to be able to copy alert text from a new Silverlight based tool and discovered much to my surprise that copying text out of Silverlight is not a straightforward process.&nbsp; <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">I will start off with the “Best” solution I was able to find and I will detail other the other less desirable option below.<o:p></o:p></SPAN></P>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Option 1:</SPAN></B><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Copy it out with JavaScript<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(Note Non IE browsers require some extra work to make this happen)<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Simply build a string that contains what you want to copy into the clipboard and call the JavaScript function.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Sample JavaScript code to place in your aspx/html file:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">head</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: red">runat</SPAN><SPAN style="COLOR: blue">="server"&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">&lt;</SPAN><SPAN style="COLOR: #a31515">script</SPAN> <SPAN style="COLOR: red">type</SPAN><SPAN style="COLOR: blue">="text/javascript"</SPAN> <SPAN style="COLOR: red">language</SPAN><SPAN style="COLOR: blue">="javascript"&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">function</SPAN> CopyToClipBoard(TextToUse)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>window.clipboardData.setData(<SPAN style="COLOR: #a31515">'Text'</SPAN>, TextToUse); <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">&lt;/</SPAN><SPAN style="COLOR: #a31515">script</SPAN><SPAN style="COLOR: blue">&gt;<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&lt;/</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">head</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">&gt;</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Silverlight C# code:<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">String</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> MyString “Some Data”;<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">HtmlPage</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.Window.CreateInstance(<SPAN style="COLOR: #a31515">"CopyToClipboard"</SPAN>, MyString);<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">The Javascript code above only works in IE, however you can find code online to enable javascript to copy to the clipboard in other browsers (See below about why they don’t make it easy).<o:p></o:p></SPAN></P>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><o:p></o:p></SPAN></B></P>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Option 2: </SPAN></B><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Place all strings you want to be copied into a TextBox. (Works with all browsers)<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Using a TextBox gives the user the ability to highlight the text and to simply do a ctrl-c to copy the text into <o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">The only object that I could find that let you select text is the TextBox object.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>So if you just want to allow the user to highlight and copy (via a ctrl-c) you can put your text in a TextBox object<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(Set it read only if you don’t want them to be able to edit it).<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">This solution has doesn’t allow you to format your text as nicely as the user is limited to only selecting only one TextBox at a time.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><FONT size=3><FONT face=Calibri>Why is this not easy to do?<o:p></o:p></FONT></FONT></B></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">The first thing you need to understand is why they didn’t make it easy for people to copy data to and from the clipboard.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The short answer is security.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>They don’t want a malicious website to be able to copy down or modify what is in the clipboard as it may contain sensitive information.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>That being said there are many times when it is useful for an application to be able to copy text to the clipboard and I have great hopes that Silverlight 3 will give the end user the option to allow/deny access to the windows clipboard.<o:p></o:p></SPAN></P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
  </channel>
</rss>