Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a new WPF form from stealing focus?

I have written a simple MSN-style program that will send and retrieve messages using WCF. The main form contains a Textbox to type in the message to be sent. In the background the application polls the server every few seconds for new messages. When a new message is received a new window is opened to display it. This has to be done on the UI thread using the Dispatcher class.

The problem is that when the new window is shown, the focus shifts away from the TextBox, so that typing is interrupted. This is very annoying! In MSN Messenger it is possible to continue typing your own message while receiving one. How is it done?

As a workaround I postpone the popup with the new message while the TextBox has focus, but there should be a better way!

like image 582
Dabblernl Avatar asked Sep 21 '09 20:09

Dabblernl


1 Answers

The answer is simple: Since .NET 3.5 SP1 WPF forms have a ShowActivated property. Set this to false and any form thus marked won't steal no focus no more!

like image 85
Dabblernl Avatar answered Sep 24 '22 17:09

Dabblernl