Application disappears in the background - c#

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.

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.

Showing form when main form gets closed

I have tried around with threads a little bit but I can't figure out the right way to get what I want (because I'm not very familiar with threads).
I have a "Sign out" button in my main form. When clicked then the Form shuts completely down and restarts (Application.Restart()). Now I wanted another form pop up when the main form gets closed (Just a label with "Loging out - Please wait"). When the main form opens then this "Log out form" should disappear.
I know I should make the Sign out form as foreground thread but how do I call the Form.Open() method with the thread and how I can get it closed when the main form starts?
Try to show the second form in the FormClosing event of the main form.
When the main form closes it most certainly "shuts down" the message loop of the application making it impossible to spawn any new window or closing any window that is already open.
Edit:
As of the comments: Showing the logout form from the process that is about to Restart until the new process has been started successfully will not be possible because the old process will be shut down and therefore any open form it shows.
Possible solutions:
Write a program with the solely purpose of showing the logout form and start that before restarting the application. After the Application has startet again search the process of the logout form and stop/kill it.
Avoid to have to restart your application completely

Prevent MessageBox from minimizing other windows

In my project I have a MessageBox that pops up from time to time.
When I'm playing a game and the MessageBox appears, the game is minimized and I'm back to the desktop.
This might depend from app to app, but this specific app/game minimizes when MessageBox appears.
How to avoid this behavior? Is there anything I can do to the MessageBox to make it lose focus/not activated ? I tried to look at the MessageBox methods but no luck.
It sounds like you want the message box to be able to display while the rest of your code is still running. Type of messagebox you are using is modal and needs to be closed until it allows you to interact with the other open window.
I suggest you start a new thread for the message box so that the thread can continue to run allowing you to be able to interact with the other windows.

Waiting for WPF window in non-WPF project

Why is it that if I call a WPF form from another project type (e.g. a console application or XNA game), the main application doesn't wait for the form to close before ending (and subsequently closing the form)?
I know with a dialog box I can make the main class wait for a reponse, how can I make it do that with my form?
You can open the window in modal mode using the ShowDialog method - the ShowDialog method only returns after the window was closed. Otherwise you can either wait until the Closed event is fired or wait until Application.Windows collection is empty (meaning the application has no WPF windows left).
For more information about window closing in WPF, refer to this.

Question about an application not getting focus in XP/W2K3

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").

Categories