简体   繁体   中英

SendInput Ctrl + C and then retrieve copied content through Clipboard.GetText doesn't work

I have a simple windows forms application with a TextBox and a Button . Clicking on the Button will put focus on the TextBox and simulate keystrokes Ctrl + C to copy the content into the clipboard. I am doing this key simulation with SendInput , also tried SendKeys.Send("^c") . This works properly for the copy operation, as I can verify by pasting the content to a notepad or something else.

I then want to programmatically retrieve this copied value through Clipboard.GetText . However, the problem is that this method always seems to return the previous value in Clipboard instead of the current one. For example, if the sequence of values for my TextBox is: "hello", "world", then the sequence of output is: junk, "hello". Below is my code using the SendKeys method for simplicity:

... highlight TextBox value...

SendKeys.Send("^c");
Thread.Sleep(100);

Console.WriteLine(Clipboard.GetText());

looks like the message loop needs to be executed before the text is placed in the clipboard

try using

  Application.DoEvents();

instead of the delay

 SendKeys.SendWait("^c");
 Clipboard.GetText(TextDataFormat.Text);

Try this:

textBox1.Focus();
Clipboard.SetText(textBox1.Text);
textBox2.Text = Clipboard.GetText();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM