2

I am working on an application which requires a form to be activated on receiving some external events. For this as a POC I have created the following sample code.

FormRecvr:

1) Create a simple form with textbox control

2) Have a TCP listner listning on some port to receive the event

3) On the reception of event, activate the form i.e bring it to front so that it has focus and user can type some text.

FormSender:

1) Create a simple form with two button controls, one to activate the rcvr form by sending an event through socket and the other to exit the application.

2) Create a TCP socket with the listner and send the event.

After executing this simple program, what I have noticed is the receiver form doesn't get the focus. The taskbar menu of that app keeps blinking. The window doesn't come as foreground.

I have tried the following to bring it to focus.

this.TopMost = true;

this.Activate();

this.TopMost = false;

I have even tried SetForegroundWindow Win32 API. Even with that it doesn't work. It works itermittently. 8 out of 10 times, it gives the above mentioned issue. The form will be visible but focus will not be there, the task bar menu will be falshing. We have to click on the form or on the flashing task menu bar to get the focus.

Please let me know how to resolve this issue.

GoGetIt
  • 21
  • 1

3 Answers3

0

Setting TopMost is unnecessary; have you tried calling Focus() on the Form?

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

Have you tried:

Form.BringToFront();

if that fails then perhaps

Form.Visible = false;

Minimize window

Form.Visible=true;

Restore window

worth a try perhaps

WraithNath
  • 17,658
  • 10
  • 55
  • 82
0

I am not entirely sure what it is you are after but stealing focus from another application is bad and should not be done/possible.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • I got your point. But in the final application user will be aware where he is clicking and his expectation is as soon as he click he should have the focus. The click event is sent to my form application through a call back mechanism. And on receiving this event, I have to activate the form so that user has the input focus and can type some text. – GoGetIt Jan 31 '11 at 05:26