Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello. I have a problem. I need to copy the text selected in any application (whether web-browser or notepad) and copy it to the clipboard. Who than can help please!
Posted

That can't be done in C# without some p/invoke. You need to use Win32 API calls to find the focused window and ask it for it's text.
 
Share this answer
 
This question makes sense to me only if: you are going to return focus after a copy operation in any application to your C# application, and then use the clipboard contents.

The only way I can imagine a solution is by implementing a GlobalSystemHook: see "Processing Global Mouse and Keyboard Hooks in C#" by George Mamaladze[^] which will notify/invoke your C# application in the case of a control-c event.

When notified of that event, you will, then, have to examine the clipboard contents to filter for text-only content ... I assume.
 
Share this answer
 
v2
As an alternative to using windows notepad.exe have you considered embedding a notepad clone into your application. I created an exact clone of notepad in C# for exactly this purpose. You can find the source code here:
http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html[^]
 
Share this answer
 
I understand it ... here and ask you to help in this. How to get a handle to the active window, I know, but while copying the selected text problem (
 
Share this answer
 
Comments
Christian Graus 10-Aug-10 4:23am    
Please don't push answer to ask a question. I know there's an API call you can use to get the text of a window, once you have the window handle ( of course, the handle to the top level window is NOT the handle to the textbox ), and I jut googled it - GetWindowText.
Toli Cuturicu 10-Aug-10 10:15am    
Reason for my vote of 1
fake answer
Hi, you can copy and past selected text to Clipboard by following:

Copy:

<br />
Clipboard.SetText(ListBox1.SelectedItem.ToString());<br />



Past:

<br />
ListBox2.Items.Add(Clipboard.GetText());<br />
 
Share this answer
 
Comments
Christian Graus 10-Aug-10 4:23am    
Reason for my vote of 1
Does not answer the question. The OP wants to get text from a window outside his app, not inside it.
Toli Cuturicu 10-Aug-10 10:17am    
Reason for my vote of 1
Read the question first!
Copy the selected text to Clipboard?
Ctrl+C?

You mean to get the data from the Clipboard or what?
 
Share this answer
 
Comments
Toli Cuturicu 10-Aug-10 10:16am    
Reason for my vote of 1
Read the question carefully or don't answer.
Clipboard.SetText ("string") use will not work because "String" is unknown to us, it is allocated in a certain application (eg Notepad)
 
Share this answer
 
Comments
Toli Cuturicu 10-Aug-10 10:16am    
Reason for my vote of 1
fake answer


The key to a solution is analysis of the problem statement. The comments about a clipboard imply a solution. The actual requirement appears to be that the customer wants to open a Notepad window with text in it, where the text is from a text box.

Process notepad = new Process();
     notepad.StartInfo.FileName = @"notepad.exe";
     notepad.EnableRaisingEvents = true;
     notepad.Start();
     notepad.WaitForInputIdle(1000);
     if (notepad.Responding)
     {   //
         // The process/window for notepad.exe is now in the foreground
         //
         System.Windows.Forms.SendKeys.SendWait(MyTextBox1.Text);
      }


Note. This solution was found on the web. It was modified for clarity, i.e. the process object is "notepad" and WaitForInputIdle was reduce to 1 second.

If someone has the time to look into an improvement to this code, then one question is, can the completion of WaitForInputIdle be trapped as an event which invokes the SendKeys.SendWait method?

 
Share this answer
 
Comments
André Kraak 21-Oct-11 18:29pm    
Reason for 1 Vote:
Does not answer the question.
You misunderstood the question. The OP is looking for a solution that allows him to copy the selected text from any active program. Notepad was just mentioned as an example.
You also turned it around, you are pasting to Notepad were the OP wants to copy from Notepad (or any other program).
Reto Ravasio 22-Oct-11 3:52am    
you're right, but the code above could be used to send ctrl-A, ctrl-C to the application. This might work for some applications.
For Copy Use

Clipboard.SetText

For Paste Use

Clipboard.GetText()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900