It is possible automatically copy text when selected or highlight I mean only selected then copy to clipboard in c# ? thank you very much like this question C# , detect selected text on windows? but I can't use the code of above answer of question
-
1Yes, it is possible. What platform? – SLaks Sep 17 '13 at 19:09
-
Metro? WinForms? WPF? Silverlight? Windows Phone? ASP.Net? – SLaks Sep 18 '13 at 00:53
-
Windows form in c# @SLaks I need just retrieve the text when I selected or I highlighted it – Seeatme Sep 18 '13 at 08:21
-
When selected in what? – SLaks Sep 18 '13 at 12:59
-
So you're actually asking about _Javascript_. – SLaks Sep 22 '13 at 01:34
3 Answers
Well, if it's a Windows Forms application, consume the GotFocus
event and issue this command:
Clipboard.SetData(((TextBox)sender).Text);
If it's a Web Forms application then you'll need to use JavaScript. You'll need to consume the focus
event of the text box:
<input type="text" focus="copyToClipboard(this);" />
and then you'll want this JavaScript:
function copyToClipboard(obj) {
var text_val=eval(obj);
text_val.focus();
text_val.select();
if (!document.all) return; // IE only
r = text_val.createTextRange();
r.execCommand('copy');
}
The problem with the JavaScript is that it doesn't actually work in every browser. See, the problem with the JavaScript approach is that you'll need code for all the browsers and you need to make sure that the user doesn't have JavaScript turned off. It progressively gets more complex.
Reference this post for more information on clipboard work in JavaScript, How do I copy to the clipboard in JavaScript?.

- 1
- 1

- 66,820
- 29
- 157
- 232
-
This javascript only works in IE (less than 10), might as well remove that bit from your answer. Don't know anyone anymore who is writing a web app for IE8 only (for example). – EkoostikMartin Sep 17 '13 at 19:52
-
@EkoostikMartin, actually I have over 14K+ customers that still use IE 8/9. I would say that the paragraph I leveraged after the code snippet is the most important part of the JavaScript implementation. Many use Flash objects - but there's a problem there because not everybody has flash. In short, the fragmentation on browsers, operating systems, and user settings make it nearly impossible to reach *everybody* with a JS implementation. – Mike Perrenoud Sep 17 '13 at 19:56
-
Forcing users towards a browser/OS is a mistake for any web app (even intranet/internal) especially if its IE. I should have said this in my original comment, but also I don't think copy/paste is relevant to any web based app. The clipboard is an OS subsystem, interaction with it should be completely restricted by any self respecting browser. Copy/Paste within the browser itself makes sense, maybe HTML6? Just my opinion of course. – EkoostikMartin Sep 17 '13 at 20:17
-
-
-
@neoistheone: `document.all` has nothing to do with `execCommand()`. You should check for `execCommand()`, not an unrelated property. – SLaks Sep 18 '13 at 12:59
-
That's why god invented the Clipboard.SetText
method http://msdn.microsoft.com/en-us/library/kz40084e.aspx It works both on Windows and Linux (Mono), I don't know about Mac OS X

- 443,496
- 30
- 428
- 555
-
2I'm pretty sure that `GetText` isn't going to **set** data on the Clipboard. – Mike Perrenoud Sep 17 '13 at 19:15
-
You right but I need get copy of text when select text only select not use right click then copy or ctr+c :) – Seeatme Sep 17 '13 at 19:19
-
You'll probably want to handle the Validate event and pass the SelectedText property to the clipboard. something like this:
Clipboard.SetData("{0}",((TextBox)sender).SelectedText);

- 6,908
- 2
- 15
- 22