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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top