Code :

SendKeys.Send("%{TAB}"); Clipboard.SetText(fromTextBox.Text); SendKeys.Send("^v"); for (int i = 0; i < 12313; i++) SendKeys.Send("{TAB}"); Clipboard.SetText(toTextBox1.Text); SendKeys.Send("^v"); 

As a result, the second text is inserted twice. Where is the mistake ?

Upd:

 fromTextBox.Text = "В лесу родилась елочка"; toTextBox1.Text = "Росла она там"; 

The above code will insert in the field of another program:

She grew there Pile of Tabulations She grew there

I need:

Herringbone was born in the forest Pile of Tabulations She grew there

  • one
    Please provide a more complete code example. And what is the second text inserted? What result do you expect, what you get? - Stepan Kasyanenko
  • As it should be: one test is copied to the buffer first, then inserted. After that, the second text is copied, then it is also inserted. But, the code above inserts the test twice from toTextBox1.Text - DenisJNewb
  • Do not reply in the comments. It is better to update your question. Best with examples of text that you paste, copy. - Stepan Kasyanenko

1 answer 1

It will be correct to use such code:

 [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); Process[] handles = Process.GetProcessesByName("notepad"); SetForegroundWindow(handles[0].MainWindowHandle); Clipboard.SetText(text); SendKeys.SendWait("^v"); 

Inserts as expected