Andrew
Andrew

Reputation: 7768

Copy Selected Area's text to clipboard

I am pretty new to C# and programming in general. I am wondering if there is a way to implement the following feature:

  1. in my C# program have a textbox (named tb1) and button (named bt1).
  2. When I click bt1, my cursor will change to "selector"
  3. I switch to program where I need the value to be taken from
  4. I select text that I need to be copied to my C# program
  5. Selected text will appear in bt1

WindowsXP/Windows Vista/7

PS - I know how to get text from the clipboard and how to set text to the clipboard, what I need is the way to capture ANY text AFTER I press the button. For example- I have 10 textboxes; each textbox has a button.

The process would be:

  1. button1 click -> select text in another application -> which is auto-pasted to textbox1

  2. button2 click -> select text in another application -> which is auto-pasted to textbox2

  3. button3 click -> select text in another application -> which is auto-pasted to textbox3

etc.

Upvotes: 5

Views: 4090

Answers (1)

Abel
Abel

Reputation: 57149

You don't specify whether the user can click Ctrl-C in step (4) when inside that other application. If so, the selected text can indeed appear in your application: all you need to do is reading the Clipboard object, as in Clipboard.GetText().

A good introduction with some how-to's can be found here at CodeProject.


In the case where step (4) in your question should auto-copy the selected text to the clipboard from a foreign application, consider using an automation-tool like AutoIt or the White Framework instead (more options in this question on automation: What's a good, if any, .NET Windows automation library?).

Upvotes: 3

Related Questions