-7

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

Community
  • 1
  • 1
Seeatme
  • 15
  • 6

3 Answers3

3

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

Community
  • 1
  • 1
Mike Perrenoud
  • 63,395
  • 23
  • 143
  • 222
  • 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
  • Don't feature-test on unrelated objects. – SLaks Sep 18 '13 at 00:54
  • @SLaks, I'm not sure I understand what you're saying. – Mike Perrenoud Sep 18 '13 at 03:11
  • @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
  • @SLaks, now I understand what you're saying. Thanks! – Mike Perrenoud Sep 18 '13 at 13:17
0

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

Willem Van Onsem
  • 321,217
  • 26
  • 295
  • 405
0

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);

tinstaafl
  • 6,610
  • 2
  • 12
  • 22