WPF SendKeys - Copy to clipboard

JElster
JElster used Ask the Experts™
on
Hi..
I have click event - I'm trying to do a copy of the selected text and place it on the clipboard.
But the following SendKeys.. does nothing... How can I fire a CTRL+C in WPF

   private void menuCopy_Click(object sender, RoutedEventArgs args)
        {

            // CTRL C
                   System.Windows.Forms.SendKeys.SendWait("^C");
 

         
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
private void menuCopy_Click(object sender, RoutedEventArgs args)
        {

//set focus to textbox where you selected text
 textBox1.Focus();
            // CTRL C
                   System.Windows.Forms.SendKeys.SendWait("^C");
 

         
        }
Commented:
and use small "C"
like..

textBox1.Focus();
            System.Windows.Forms.SendKeys.SendWait("^c");  
Commented:
I don't think that using SendKeys is the best solution...
Is there any reason you can't use ApplicationCommands.Copy?
Something like ApplicationCommands.Copy.Execute(null,this)....
If (as I suspect from function name) your function is actually a handler for some contextmenu item, you can just put in Xaml something like this:
<MenuItem Header="Copy" Command="ApplicationCommands.Copy" />

Or maybe just Clipboard.SetText ?
I would fire the keystrokes only as a last resort...
Good luck.

Author

Commented:
I tired Application.Copy but nothing got copied to the clipboard  ?????

Commented:
I think that the syntax is ApplicationCommands.Copy and not Application.Copy.
If this is what you meant than probably the reason is that the control in which your text is selected has no focus when the command is executed.
You can try snoop utility to see which element got focus each time and then set some element which steals the focus
as Focusable=false.
Or you can force the focus as Binuth suggested previously.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial