Copying to the clipboard

J

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
 
S

Scott M.

If it isn't recognizing the name Clipboard, then you haven't got the
namespace that contains the Clipboard class imported.
 
J

Jeff

Scott M. said:
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
 
N

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
 
K

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.)
 
J

Jeff

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
 
K

Kevin Yu [MSFT]

S

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>
 
J

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

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
 
S

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
 
J

Jeff

Scott M. said:
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
 
S

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.
 
J

Jeff

Scott M. said:
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
 
J

Jeff

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
 
K

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.)
 
J

Jeff

Kevin Yu said:
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


I just tested this and the above works and it is easier and shorter than
mine and saves a postback. Thanks.

I was not familar with the document.getElementByID code structure.

I assume that I can use similar code to get values from other dot.net
controls (like radiobuttons, hiddenfields, etc) and use those values in
javascript?
Can the value from a session variable also be passed to client-side
javascript in some easy and similar manner?

Jeff
 
S

Scott M.

The problem is you are using a textbox control when you should be using a
textarea control. In your ASP.NET, start with a textbox control, but then
set the "multiline" property to true and this will render to the client as
an HTML <TEXTAREA> control. TextArea controls support the use of the ENTER
key and do not cause postbacks as a result. They also allow for multiple
lines of text, which will make the code I gave you work just fine.
 
S

Scott M.

Inline...

Jeff said:
I just tested this and the above works and it is easier and shorter than
mine and saves a postback. Thanks.

I was not familar with the document.getElementByID code structure.

getElementById is a method of the document object in the Document Object
Model (which is a standard object model for parsing markup).
I assume that I can use similar code to get values from other dot.net
controls (like radiobuttons, hiddenfields, etc) and use those values in
javascript?

You've got to remember that this code is used in your JavaScript
(client-side) and therefore, when the client is doing any operation, it
doesn't know anything about server-side controls or server-side code. To
the client, there is only client-side code. The ASP.NET controls you add to
your page during development render to the client as client-side HTML
controls, and so, yes, the DOM works with client-side code.
Can the value from a session variable also be passed to client-side
javascript in some easy and similar manner?

Server side values can be passed to the client in a number of ways, but one
easy way is to create a hidden form field to your page, mark it with
runat="server" and give it an id. Then you can place your server-side value
into it, have that data passed down to the client and have your client-side
code retrieve it just as the textbox value is retrieved.
 
K

Kevin Yu [MSFT]

Hi Jeff,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

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

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top