Question about an application not getting focus in XP/W2K3 - c#

I have an application written in .NET. The previous version had no problems: you double-click on the icon or run it from a command line and when it starts up, it's the main window and has focus as you'd expect.
The latest version displays a splash screen before the main window and now the splash screen comes to the foreground ok but the main one does not always end up the main window. Sometimes it does, sometimes it doesn't. (When launched from the command line it invariably doesn't). When the main window does not come to the foreground and get focus, the taskbar icon shows as a steady orange.
I see lots of hits on the net about how MS added a facility to prevent applications stealing focus from others, centered around the ForegroundLockTimeout registry setting and related settings, but the behaviors described above for the different versions happen on the same machine.
I have tried called Activate in the main form when it finally gets created, and also SetForegroundWindow, all to no avail.
Any help is appreciated.

You should probably have the splash screen set focus to the main app window as it is going away.
As far as Window's knows your splash screen IS your app since it's the first toplevel window shown after the process is started. So that window gets the focus, but any OTHER window trying to grab focus on that same app startup (the icon click/run command) is believed to be a focus thief.
You can get around this by having the window that Window's believes has a right to have focus be the one to transfer focus to a new window.
So you splash should SetFocus on the main window BEFORE the splash is destroyed. If you destroy the focus window, then the focus is nowhere, which is probably what is happening currently in your app.

Sounds like your main window doesn't always get the focus back from the splash screen. Have you tried simply calling SetFocus in the main form's OnLoad handler?MSDN

I just corrected a similar problem. Just make sure your splash screen closes after your main form as shown (it will show in background of the splash if your splash is "always on top").

Related

Winforms ShowDialog(this) blocks the switching windows of other applications to the foreground

When I open any dialog in my Winforms application then Windows 10 behaves oddly in these ways:
ALT-TAB no longer displays the list of windows
I cannot switch to hidden applications using taskbar icons - if I select a taskbar icon of a window that is not currently visible (even if it is not hidden by the winforms app) the icon just flashes and then stays highlighted, but is not brought to the foreground. I can use other applications that are visible. As soon as I close the dialog form the other I can use the windows of other applications correctly
If I switch to the application window that is behind the winforms application by clicking on it, I cannot go back to the winforms app either by ALT-TAB or by clicking on the taskbar icon. I can get back to it by minimizing the other application
I'm opening the dialogs with
dialogFormName.ShowDialog(this);
TopMost is set false on all forms and is not set in the code.
I've read about 50 related articles and the only problems seem to be either TopMost is set or ShowDialog is not called with the parent form. I'm not a regular Winforms developer so I'm probably doing something stupid. This is driving me crazy, so I'd really appreciate any help!
Edit: The same issues occur with MessageBox.Show(this, "test"). The issue does not occur with a newly created app just with a single button calling MessageBox.Show(this, "test"). The problem application uses EntityFramework but no other packages and the problem existed before I added EF.
After trying different scenarios I eventually found the issue. In my case I was calling ShowDialog() after a user clicks an item on a ContextMenu. The blocking of ALT-TAB was caused by the following code that attached the ContextMenu to the ListView that the menu was contextually for:
lstList.ContextMenu = myContextMenu;
As soon as I removed that association, the ShowDialog no longer blocked ALT-TAB.
Form.ShowDialog() blocks the parent form until it's closed.
Use Show() to display the form separately without blocking its parent.

Can't restore minimized WinForms app from taskbar

I have a very strange problem, in which a standard WinForms C# application can sometimes not be restored from the taskbar, after it has been minimized or if another application was used in maximized mode.
This happens on Windows Server 2012 and on Windows 10 (I haven't tested it in other OS'es). If the mouse hovers over my applications taskbar icon, a thumbnail view of my application shows up. Moving the mouse cursor up, to hover over the thumbnail, the application actually shows up on the desktop again, but if I left click on the thumbnail or moves the cursor off the thumbnail, it disappears again. If instead I right click the thumbnail, I can choose "Maximize" or "Restore" from the popup menu, and either works as expected. So the issue only affects regular left clicking on the taskbar icon or the popup thumbnail.
I'm using .NET Framework 4.6 for my application, and the application consists only of a single form with default property values and no WndProc overrides or other fancy things.
I have no idea how to debug this, and the bug is not reproducible 100% of the times, so any suggestions on how I can capture and provide more information to help solve the issue would be very welcome.
For me, the problem was an always visible modeless Form that was owned by the main window. Apparently when the main window is minimized, the modeless form needs to be minimized as well. WinForms doesn't do this automatically for you. I had to subscribe to the main window Resize event, and set the state in there:
private void MainWindow_OnResize(object sender, EventArgs e)
{
modelessForm.WindowState = WindowState;
}
This issue was not encountered on Windows 7, only on Windows 10.

Why does my form sometimes vanish when closing a dialog?

Occasionally, seemingly randomly, when I close a dialog form my main form seems to move back in the window order, disappearing behind the next application back (usually Visual Studio). It retains focus, so clicking it in the taskbar minimises it, requiring another click. Whenever this happens, the control colours seem to change a little as well, but revert if I maximise and restore the window.
Any idea why this happens?
Edit: This happens when I'm debugging; it might happen at runtime too, but I usually don't have anything behind the application then, so I haven't noticed. Nobody has mentioned it.
This will happen when the dialog closes and Windows cannot find any window in your app that isn't disabled. Forced to move the focus somewhere, it will pick a window of another app to give the focus to. Your form will disappear behind it.
Exactly why your main form is disabled when this happens isn't clear. The color change certainly suggests you are changing the Enabled property of the form. Everything turns battle-ship gray when you do that. Setting Enabled back to true after the dialog closes doesn't work, it's too late. Just don't tinker with Enabled, the ShowDialog() method already disables other windows.

Application disappears in the background

in my application there is a print form. When you click the print button, the windows print dialog appears, showing the printed page.
When it's finished the print form is closed (this is still ok) but my application is set to the background on the z-order (this is not ok) and another application window is set to the foreground.
I could help myself by calling WinAPI-SetForegroundWindow() from my application, but it flickers and I don't think that this is a clean approach.
A clean approach would be that my application doesn't get set to the background.
Any suggestions?
try to call .ShowDialog with your form as the first parameter. that should bring your form into the front after the dialog is closed.

Keep window in foreground (even if it loses focus)

In my application (C#, Windows Forms) I have got a telephone information screen: if there is an incoming phone call a window with additional information to the caller is shown.
This window should open in foreground and should stay there even if the user is writing in another window (so the user still sees the information regarding the incoming phone call).
First I've used BringToFront - but this didn't really work in all circumstances (e.g. when the main application was not in the foreground).
Right now I am using BringToFront and the user32.dll function BringWindowToTop -> which works, but if the window loses focus it is in the background.
How do I implement this behaviour ?
I think you want to set your form as TopMost:
yourForm.TopMost = true;

Categories