Click here to Skip to main content
15,886,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a win app (C#) that use clipboard to send and receive data to/from other applications. for example i wnat to use Word app in windows, I copy a text using c# to the clipboard, but when i want to simulate paste key (Stroke Ctrl+v) in c# , the clipboard is empty and i just got "v" as result.
To copy to the clipboard i use the following code:
C#
public static void CopyToClipboard(string textForCopyToClipBoard)
{
  Clipboard.Clear();
  Clipboard.SetDataObject(
        textForCopyInClipBoard, 
        true, 
        5, 
        200); 
}


To simulate paste or stroke Ctrl+v, i use the following code
C#
public static void PasteFromClipboard()
       {
           System.Windows.Forms.SendKeys.Send("^v");
       }


What I have tried:

To copy to the clipboard, I tried the following method
Posted
Updated 17-Jul-16 4:13am

1 solution

Start by using Clipboard.SetText instead of SetDataObject - it shouldn't make any difference in this case, but it's a simpler way to do things.
Your code works: I tried it in a simple form in one of my apps:
C#
private void button2_Click(object sender, EventArgs e)
    {
    tbPasteIntoMe.Focus();
    Clipboard.SetText("Hello there!");
    SendKeys.Send("^v");
    }
And my textbox gets the string "Hello there!" without problems.
But...do remember that SendKeys always sends to the Active application - which is probably yours, not the one you want to paste into. This may have something to do with your problem...

But to be honest, you shouldn't use the clipboard unless the user has explicitly told you to - many users (including me) will get very, very annoyed if you overwrite what they have placed on the clipboard without asking them in advance.
You probably should find a better method for interprocess communications - using the clipboard is what I call a "brute force and ignorance" approach which generally means your whole design needs looking at.
 
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