1

I am trying to make updates to two linked TextBoxes. I disable events in one and then send keystrokes using eg SendKeys::Send("A"); having first given it focus:

texBox2->Focus();
texBox2->KeyDown -= gcnew KeyEventHandler(this, &Form1::texBox2_KeyDown);
SendKeys::Send("A");
texBox2->KeyDown += gcnew KeyEventHandler(this, &Form1::texBox2_KeyDown);

It almost works but goes totally mental instead repeating the character (I daren't look to check which exact key because I'm frantically firefighting an overflow) until I press control-alt-del. No other keys have any effect and the mouse freezes up. But task manager miraculously restores my control, I don't stop or kill anything from it.

Can anyone advise? The debugger hangs on that SendKeys::Send("A"); statement.

John
  • 6,433
  • 7
  • 47
  • 82

1 Answers1

5

SendKeys places input in the message queue which is queued and so will get processed after you have reconnected the events. Hence the wackiness.

My advice is to stop using SendKeys to update the contents of your own controls. Simply modify the contents of the text boxes directly.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Direct modification of large immutable strings causes flicker, I have found the SendWait method more promising. I had set a break point in each handler and it wasn't breaking to them while in the 'infinite' loop. – John Oct 10 '11 at 20:30
  • Assuming this is WinForms then you can append characters cheaply with the windows api using WM_SETSEL I think. There may be a .net way to do it. Reassigning the whole string is bad but SendKeys is worse. Use WM_SETSEL is my top tip here. – David Heffernan Oct 10 '11 at 20:35
  • But how right you are. Even more wackiness after initial satisfaction with SendWait. The whole screen (not just textboxes) flicker and if I hold a key down it appears out of place as I'm updating the second programmatically. No massive loops though :) Thanks for the WM_SETSEL tip David – John Oct 10 '11 at 20:36
  • 1
    actually EM_SETSEL and EM_REPLACESEL are the messages. – David Heffernan Oct 10 '11 at 20:40