Home All Groups Group Topic Archive Search About

Copying to the clipboard

Author
14 Jan 2007 7:18 PM
Jeff
....working with visualweb.net 2005 and vb.

....trying to simply copy the contents from a textbox to the clipboard.

I've looked at a large number of places on line and they give me various
code, but it doesn't work. I'm apparently missing some type of declaration,
and the code is diffent in visualweb.net than elsewhere.
When I try the code below, it tells me that the "name clipboard is not
declared"

What am I missing?

Thanks

Jeff


Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCopy.Click
           Clipboard.SetDataObject(TextBox1.Text)
End Sub



--
Posted via a free Usenet account from http://www.teranews.com

Author
14 Jan 2007 8:08 PM
Scott M.
If it isn't recognizing the name Clipboard, then you haven't got the
namespace that contains the Clipboard class imported.


Show quoteHide quote
"Jeff" <n***@none.com> wrote in message
news:45aa7550$0$4845$88260bb3@free.teranews.com...
>
> ...working with visualweb.net 2005 and vb.
>
> ...trying to simply copy the contents from a textbox to the clipboard.
>
> I've looked at a large number of places on line and they give me various
> code, but it doesn't work. I'm apparently missing some type of
> declaration, and the code is diffent in visualweb.net than elsewhere.
> When I try the code below, it tells me that the "name clipboard is not
> declared"
>
> What am I missing?
>
> Thanks
>
> Jeff
>
>
> Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles BtnCopy.Click
>           Clipboard.SetDataObject(TextBox1.Text)
> End Sub
>
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Author
14 Jan 2007 8:47 PM
Jeff
"Scott M." <s-mar@nospam.nospam> wrote in message
news:eUSd$eBOHHA.4848@TK2MSFTNGP04.phx.gbl...
> If it isn't recognizing the name Clipboard, then you haven't got the
> namespace that contains the Clipboard class imported.

Thanks, but I'm still new at this. Could you please explain how to do that?

Jeff


--
Posted via a free Usenet account from http://www.teranews.com
Author
15 Jan 2007 2:20 AM
Kevin Yu [MSFT]
Hi Jeff,

To use the clipboard in a windows form app, you have to reference to the
System.Windows.Forms assembly. By default, when you create the project, it
was automatically added. If it isn't there, you can add it using Add
Referece.

Then all you need to do is to use SetDataObject method. If you didn't
import the namespace, you just need to add namespace before the class name,
like the following.

Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCopy.Click
           System.Windows.Forms.Clipboard.SetDataObject(TextBox1.Text)
End Sub

By the way, this can only be used in a windows form app. For web app, doing
clipboard operations at client side involves scripting.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
15 Jan 2007 3:47 AM
Jeff
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:W5eyIvEOHHA.2300@TK2MSFTNGHUB02.phx.gbl...

> By the way, this can only be used in a windows form app. For web app,
> doing
> clipboard operations at client side involves scripting.
>
> Kevin Yu
> Microsoft Online Community Support


Okay, this must be the problem. I found similar instructions to what you and
the other poster provided, but I am attempting this on a web app.

Is there an easy way get the text from a dot.net text box in a web app into
the clipboard using javascript or similar script after clicking a dot.net or
html button?

Jeff



--
Posted via a free Usenet account from http://www.teranews.com
Author
15 Jan 2007 5:51 AM
Kevin Yu [MSFT]
Hi Jeff,

On the webpage, you have to achieve this with client side scripts. Here are
many examples for your refrence. If anything is unclear, please feel free
to let me know.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0813.
mspx

http://www.htmlgoodies.com/beyond/javascript/article.php/3458851

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
15 Jan 2007 5:18 PM
Jeff
Show quote Hide quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:oMy%23IlGOHHA.3604@TK2MSFTNGHUB02.phx.gbl...

> On the webpage, you have to achieve this with client side scripts. Here
> are
> many examples for your refrence. If anything is unclear, please feel free
> to let me know.
>
> http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0813.
> mspx
>
> http://www.htmlgoodies.com/beyond/javascript/article.php/3458851
>
> Kevin Yu

I get the general idea, but I'm new to VB and even newer to scripting so I'm
missing one or two details.
The first page you pointed to above looks promising for my application. What
I'm doing is having the application produce text that is visible in a
textbox - that part's okay. I would then like the user to be able to click a
button and have that text copied to the clipboard. I tried the code below
that is similar to what was suggested on the first page. I get no warnings
during development, but when I attempt to run it, the client side browser
produces an error that the active-X component could not be created (IE7 on
vista rc1 - I haven't tested elsewhere).

What might I be still missing?
Jeff

    Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCopy.Click
        Dim objIE
        Dim strCopy As String

        strCopy = "This text has been copied to the clipboard."

        objIE = CreateObject("InternetExplorer.Application")
        objIE.Navigate("about:blank")
        objIE.document.parentwindow.clipboardData.SetData("text", strCopy)
        objIE.Quit()

    End Sub



--
Posted via a free Usenet account from http://www.teranews.com
Author
15 Jan 2007 7:31 PM
Scott M.
I wouldn't try to solve this with server-side VB.NET code.  As pointed out,
the clipboard is a client-side object and so the best place to put your code
is on the client (JavaScript).

Simply add the following JavaScript function to your HTML (in the <HEAD>
section):

<SCRIPT LANGUAGE="JavaScript">

function ClipboardSend()
{
clipboardData.setData("text",txtTest.value)
}

</SCRIPT>


Then, further down in your HTML (probably right after the code for your
textbox), add the following code to create the client-side button that takes
the text from your textbox and puts it on the clipboard:

<Input type="button" onClick="ClipboardSend()" Value="Copy To Clipboard">

That should do it.

Good luck,

-Scott


Show quoteHide quote
"Jeff" <n***@none.com> wrote in message
news:45abaaa1$0$4881$88260bb3@free.teranews.com...
>
> "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
> news:oMy%23IlGOHHA.3604@TK2MSFTNGHUB02.phx.gbl...
>
>> On the webpage, you have to achieve this with client side scripts. Here
>> are
>> many examples for your refrence. If anything is unclear, please feel free
>> to let me know.
>>
>> http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0813.
>> mspx
>>
>> http://www.htmlgoodies.com/beyond/javascript/article.php/3458851
>>
>> Kevin Yu
>
> I get the general idea, but I'm new to VB and even newer to scripting so
> I'm missing one or two details.
> The first page you pointed to above looks promising for my application.
> What I'm doing is having the application produce text that is visible in a
> textbox - that part's okay. I would then like the user to be able to click
> a button and have that text copied to the clipboard. I tried the code
> below that is similar to what was suggested on the first page. I get no
> warnings during development, but when I attempt to run it, the client side
> browser produces an error that the active-X component could not be created
> (IE7 on vista rc1 - I haven't tested elsewhere).
>
> What might I be still missing?
> Jeff
>
>    Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles BtnCopy.Click
>        Dim objIE
>        Dim strCopy As String
>
>        strCopy = "This text has been copied to the clipboard."
>
>        objIE = CreateObject("InternetExplorer.Application")
>        objIE.Navigate("about:blank")
>        objIE.document.parentwindow.clipboardData.SetData("text", strCopy)
>        objIE.Quit()
>
>    End Sub
>
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Author
15 Jan 2007 11:29 PM
Jeff
"Scott M." <s-mar@nospam.nospam> wrote in message
news:%23Gdw2uNOHHA.1240@TK2MSFTNGP03.phx.gbl...
>I wouldn't try to solve this with server-side VB.NET code.  As pointed out,
>the clipboard is a client-side object and so the best place to put your
>code is on the client (JavaScript).

Okay, I have most of this figured out by combining your suggested code with
a few other things that I've tried.
I already have vb.net code that extensively manipulates text in TextBox1, so
I would prefer to keep the asp.net textbox1 as is.
The code below works to combine the client side javascript with the vb.net
code as long as the text in textbox1 is on a single line.
If the text in textbox 1 is on multiple lines, the code fails.

Any idea how I might modify it so that it works with multiple line textbox
text? I apparently need to replace the line feeds with control characters
that the javascript can handle, but I'm not sure exactly how.

Jeff


Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCopy.Click
        Dim sb As New StringBuilder
        sb.Append("<script language='JavaScript' type='text/javascript'>")
        sb.Append("{clipboardData.setData('text', ")
        sb.Append("'" & TextBox1.Text & "')}")
        sb.Append("</script>")
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "nothing",
sb.ToString)
    End Sub



--
Posted via a free Usenet account from http://www.teranews.com
Author
16 Jan 2007 12:04 AM
Scott M.
Hi Jeff,

First off, the code I gave you will work with whatever VB.NET code you've
written for your textbox.  Using the code I gave you doesn't mean that you
have to change anything about your textbox.

Second, if you are using the value property of a textbox in your client
code, it will get all of the text in the textbox, regardless of how many
lines it is on.


Show quoteHide quote
"Jeff" <n***@none.com> wrote in message
news:45ac01ac$0$4776$88260bb3@free.teranews.com...
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:%23Gdw2uNOHHA.1240@TK2MSFTNGP03.phx.gbl...
>>I wouldn't try to solve this with server-side VB.NET code.  As pointed
>>out,
>>the clipboard is a client-side object and so the best place to put your
>>code is on the client (JavaScript).
>
> Okay, I have most of this figured out by combining your suggested code
> with a few other things that I've tried.
> I already have vb.net code that extensively manipulates text in TextBox1,
> so I would prefer to keep the asp.net textbox1 as is.
> The code below works to combine the client side javascript with the vb.net
> code as long as the text in textbox1 is on a single line.
> If the text in textbox 1 is on multiple lines, the code fails.
>
> Any idea how I might modify it so that it works with multiple line textbox
> text? I apparently need to replace the line feeds with control characters
> that the javascript can handle, but I'm not sure exactly how.
>
> Jeff
>
>
> Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles BtnCopy.Click
>        Dim sb As New StringBuilder
>        sb.Append("<script language='JavaScript' type='text/javascript'>")
>        sb.Append("{clipboardData.setData('text', ")
>        sb.Append("'" & TextBox1.Text & "')}")
>        sb.Append("</script>")
>        ClientScript.RegisterClientScriptBlock(Page.GetType(), "nothing",
> sb.ToString)
>    End Sub
>
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Author
16 Jan 2007 12:52 AM
Jeff
"Scott M." <s-mar@nospam.nospam> wrote in message
news:eXvkVHQOHHA.3212@TK2MSFTNGP02.phx.gbl...
> Hi Jeff,
>
> First off, the code I gave you will work with whatever VB.NET code you've
> written for your textbox.  Using the code I gave you doesn't mean that you
> have to change anything about your textbox.
>
> Second, if you are using the value property of a textbox in your client
> code, it will get all of the text in the textbox, regardless of how many
> lines it is on.

I can't get your code to work as written, at least to do what I want. The
plain html text input will not accept multiple lines of text. ...just in
testing alone, if you attempt to hit the enter key, it causes a postback.
What I need to do is to copy a large number of lines to the clipboard. The
code that I posted previously works with the exception that I need to
replace vbcrlf with something that will work in javascript. ...and I can't
find any reference to what that might be. There is some note about using /r
/n, but I can't seem to get anything to work correctly.

Jeff






--
Posted via a free Usenet account from http://www.teranews.com
Author
16 Jan 2007 1:31 AM
Jeff
"Jeff" <n***@none.com> wrote in message
news:45ac152c$0$4830$88260bb3@free.teranews.com...

The following code works to combine client side javascript with vb.net code
to copy from a multiline textbox to the clipboard with the click of a
button.
The Javascript requires the replacement of the cursor-return/line-feeds with
\r\n and the single quotes with \'
Combining that with the clipboardData.set code and clientscript.register
code gives me code that will work to do what I want.
There probably is some more elegant way of doing this, but the entire thing
fits in 9 lines and it could have been less.

Thanks for the help.
Jeff


  Protected Sub BtnCopy_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCopy.Click

        Dim textboxvalue As String
        textboxvalue = Replace(TextBox1.Text, vbCrLf, "\r\n")
        textboxvalue = Replace(textboxvalue, "'", "\'")

        Dim sb As New StringBuilder
        sb.Append("<script language='JavaScript' type='text/javascript'>")
        sb.Append("{clipboardData.setData('text', ")
        sb.Append("'" & textboxvalue & "')}")
        sb.Append("</script>")

        ClientScript.RegisterClientScriptBlock(Page.GetType(), "nothing",
sb.ToString)
    End Sub



--
Posted via a free Usenet account from http://www.teranews.com
Author
16 Jan 2007 2:55 AM
Kevin Yu [MSFT]
Hi Jeff,

The script cannot handle multiline text because the CR control character
makes the generated script into a new line. So the script is end with
invalid char. Unfortunately, there is no build in methods to replace the
control chars in .NET framework. We have to do it with our own code. Here
is an example.

http://www.aspcode.net/articles/l_en-US/t_default/.NET/C_-encode-a-string-fo
r-JSON-JavaScript_article_398.aspx

In my opinion, we don't need to register this from server side code, since
it will be too difficult, and we're not doing any thing with the textbox
texts. You can do this directly in the .aspx file with the following
scripts. This will also save a postback to server. If you have specific
reason that you must register it from server side code, please let me know.

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function ClipboardSend()
{
   clipboardData.setData("text", document.getElementById("TextBox1").value);
}
</SCRIPT>

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
15 Jan 2007 4:59 PM
Scott M.
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function ClipboardSend()
{
clipboardData.setData("text",txtTest.value)
}

</SCRIPT>
</HEAD>
<BODY>

<Input type="text" ID="txtTest">
<Input type="button" onClick="ClipboardSend()" Value="Copy To Clipboard">

</BODY>
<HTML>


Show quoteHide quote
"Jeff" <n***@none.com> wrote in message
news:45aaecaa$0$4801$88260bb3@free.teranews.com...
>
> "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
> news:W5eyIvEOHHA.2300@TK2MSFTNGHUB02.phx.gbl...
>
>> By the way, this can only be used in a windows form app. For web app,
>> doing
>> clipboard operations at client side involves scripting.
>>
>> Kevin Yu
>> Microsoft Online Community Support
>
>
> Okay, this must be the problem. I found similar instructions to what you
> and the other poster provided, but I am attempting this on a web app.
>
> Is there an easy way get the text from a dot.net text box in a web app
> into the clipboard using javascript or similar script after clicking a
> dot.net or html button?
>
> Jeff
>
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Author
15 Jan 2007 12:22 AM
Newbie Coder
Jeff,

See if you have a reference to the 'System.Windows.Forms' class for a start

Then you can use your code as normal to copy to the clipboard

If you then want to paste the clipboard data then use this code:

        Dim iData As IDataObject = Clipboard.GetDataObject()
        TextBox1.Text = CType(iData.GetData(DataFormats.Text), String)

I used TextBox1 to hold the pasted text

I hope this helps,

Newbie Coder