C# 使用按钮 onclick 事件从文本框中复制文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17104495/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy Text from text box with button onclick event
提问by Amrit Sharma
I like to copy the text of the Textboxwhen user click button1, so that it can be paste anywhere.
我喜欢复制Textbox用户单击时的文本button1,以便它可以粘贴到任何地方。
I searched on google for some solutions, but didn't get any positive response.
我在谷歌上搜索了一些解决方案,但没有得到任何积极的回应。
Anybody advice me to how perform this action?
有人建议我如何执行此操作吗?
回答by Romano Zumbé
In the Click Event of the button use the following:
在按钮的 Click 事件中使用以下内容:
Clipboard.SetText(textBox.Text);
回答by avi.tavdi
You wish to copy text to the clipboard.
The basic syntax is:
您希望将文本复制到剪贴板。基本语法是:
Clipboard.SetText("The text you want to copy");
But in order for it to work there is more work to put into it, use the links I provided. You can find further information hereand herefor c# and herefor ASP.net which is more relevant for you.
但是为了让它工作,还有更多的工作要做,请使用我提供的链接。您可以在此处和此处为 c# 和此处为 ASP.net找到更多信息,这与您更相关。
This code was taken from CodeProject link aformentioned, and should work by using different thread.
此代码取自上述 CodeProject 链接,应该使用不同的线程工作。
private static string _Val;
public static string Val
{
    get { return _Val; }
    set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{            
    Val = label.Text;
    Thread staThread = new Thread(new ThreadStart (myMethod));
    staThread.ApartmentState = ApartmentState.STA;
    staThread.Start();
}
public static void myMethod()
{
    Clipboard.SetText(Val);
}
回答by aiapatag
You must do this on the client side (your browser). Doing this in server side (ASP.NET) doesn't make sense.
您必须在客户端(您的浏览器)执行此操作。在服务器端 (ASP.NET) 中执行此操作没有意义。
Unfortunately, clipboard manipulation is not cross-browser. If you need it to be cross-browser, you have to use flash. Look at ZeroClipboard library.
不幸的是,剪贴板操作不是跨浏览器的。如果你需要它是跨浏览器,你必须使用闪存。查看ZeroClipboard 库。
Look at this jsfiddlefor a working example.
看看这个jsfiddle一个工作示例。
<script type="text/javascript" src="http://www.steamdev.com/zclip/js/jquery.zclip.min.js"></script>
<a id='copy' href="#">Copy</a>
<div id='description'>this seems awesome</div>
$(document).ready(function(){
        $('a#copy').zclip({
            path:'http://www.steamdev.com/zclip/js/ZeroClipboard.swf',
            copy:$('div#description').text()
        });
});
Then for further examples on how to use ZeroClipboard, look at their md.
然后有关如何使用 ZeroClipboard 的更多示例,请查看他们的md。
回答by Tiny Hacker
You can use like this one:
你可以像这样使用:
private void btnCopy_Click(object sender, EventArgs e)
{
    Clipboard.SetText(txtClipboard.Text);
}
private void btnPaste_Click(object sender, EventArgs e)
{
    txtResult.Text = Clipboard.GetText();
}
回答by Sasha Xen Promychev
Clipboard.SetText(textBox1.Text.ToString()); Everyone forgot to tell you about .ToString() method. That works 100%
Clipboard.SetText(textBox1.Text.ToString()); 每个人都忘了告诉你 .ToString() 方法。这 100% 有效

