Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Win7
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 9-Aug-10 19:49pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 (
  Permalink  
Comments
Christian Graus at 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 at 10-Aug-10 10:15am
   
Reason for my vote of 1
fake answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi, you can copy and past selected text to Clipboard by following:

Copy:


Clipboard.SetText(ListBox1.SelectedItem.ToString());



Past:


ListBox2.Items.Add(Clipboard.GetText());
  Permalink  
Comments
Christian Graus at 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 at 10-Aug-10 10:17am
   
Reason for my vote of 1
Read the question first!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Copy the selected text to Clipboard?
Ctrl+C?
 
You mean to get the data from the Clipboard or what?
  Permalink  
Comments
Toli Cuturicu at 10-Aug-10 10:16am
   
Reason for my vote of 1
Read the question carefully or don't answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Clipboard.SetText ("string") use will not work because "String" is unknown to us, it is allocated in a certain application (eg Notepad)
  Permalink  
Comments
Toli Cuturicu at 10-Aug-10 10:16am
   
Reason for my vote of 1
fake answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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?

  Permalink  
Comments
André Kraak at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

For Copy Use
 
Clipboard.SetText
 
For Paste Use
 
Clipboard.GetText()
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 329
1 OriginalGriff 275
2 Richard MacCutchan 220
3 Jared Sanow 145
4 Vinay Mistry 143
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100