WinForms Form doesn't show after activation - c#

I have a WinForms Form I call from two places, one from a tray-icon's context menu and from a context menu I get from right-clicking on my app's shortcut on the desktop. When I use the tray-icon's menu the form opens just fine, even if it was minimized - it displays again. The problem is when I use the link. If the form is minimized, sometimes it will restore it, but other times it won't. The application's icon on the task-bar will flash but the window itself won't show.
I checked through debugging and both calls to display the form are from the same thread. Are there other contexts I'm missing that might interfere?
Code I use:
public void Display()
{
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
this.Focus();
}

Related

Called WinForm not getting focus

In the form calling the 2nd form I have this code:
this.WindowState = FormWindowState.Minimized;
frmCalledForm frm = new frmCalledForm();
frm.Show();
this.WindowState = FormWindowState.Normal;
And in the Load() event of frmCalledForm
this.TopMost = true;
this.BringToFront();
this.Activate();
I have the code calling the 2nd form in a button click event and at one point I open it programmatically based on certain conditions.
In both cases, the called form does not get focus, the calling form retains focus, even though it is minimized and then returned to normal.
I found the above code in this SO question, and apparently it's working for him, but for some reason it's not working for me.
Due to Chris's comment about returning the calling form to Normal state, I commented that line out, but left the line minimizing the calling form.
Besides the fact that, to the user, the main form will suddenly disappear to his taskbar and stay there, which obviously doesn't make for a good user experience. But when I ran the program with the modified code, the main form went to the taskbar, and the called form opened, but still did not have focus. I was quite surprised to see that.

WPF - can't close window if clicking outside boundaries

I have a window that I open up with ShowDialog(). The window will open in te center of my previous window. To close this window with background click I found the solution:
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
Close();
}
The solution only works (look in picture the bracket on bottom) if I click outside any of my windows. But if I click on my Window(right bracket) I only get that sound and nothing happens(OnDeactivated does not proc).
Any Ideas?
Because ShowDialog prevents you from activating dialog's parent window, hence no activation change is made, hence no proccing.
You could use Show, as in non-modal way to show your dialog form, in case you don't "wait" on the calling code for the dialog to finish with some result.
If you do, there are workarounds, like a callback solution, or a how to close a WPF Dialog Window when the user clicks outside it link that #BWA pointed out.

C# wpf multiple windows on task bar

I created a C# project which have multiple windows. When I do some other stuff, some of the windows are lost focus. Then I have to bring them to front by clicking the task bar one by one.
I am wondering is there are OnTaskBarClick Event on C# wpf window?
Then What I need to do is to set all sub windows with "ShowInTaskbar="False"". Then when ever my mainwindow is clicked on the task bar, I bring all the windows to the front.
You'll want to use the Window.Activated event to detect when your application's window is brought into focus:
From the documentation:
A window is activated (becomes the foreground window) when:
The window is first opened.
A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.
A user clicks the window's taskbar button.
Or you could use the Application.Activated event.
Clicking on a control of an already active window can also cause the Window.Activated event to fire, so if the issue is that it's firing too often, you'll probably want to watch for when the application toggles between being active and deactive.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Application.Current.Activated += CurrentOnActivated;
Application.Current.Deactivated += CurrentOnDeactivated;
}
private bool isDeactivated = true;
private void CurrentOnDeactivated(object sender, EventArgs eventArgs)
{
isDeactivated = true;
//handle case where another app gets focus
}
private void CurrentOnActivated(object sender, EventArgs eventArgs)
{
if (isDeactivated)
{
//Ok, this app was in the background but now is in the foreground,
isDeactivated = false;
//TODO: bring windows to forefont
MessageBox.Show("activated");
}
}
}
I think what you are experiencing is the window Owner not being set on your child window.
Please refer to Window.Owner Property
When a child window is opened by a parent window by calling
ShowDialog, an implicit relationship is established between both
parent and child window. This relationship enforces certain behaviors,
including with respect to minimizing, maximizing, and restoring.
-
When a child window is created by a parent window by calling Show,
however, the child window does not have a relationship with the parent
window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be
minimized, maximized, and restored independently of the other.
You can easily fix this by setting the Owner property in the child window when before calling Show() or ShowDialog()
Window ownedWindow = new Window();
ownedWindow.Owner = this; // this being the main or parent window
ownedWindow.Show();
Note : if conforming to an MVVM pattern, this can become a little
more cumbersome, however there is plenty of resources on how to link owner and parent windows.

Process continue after closing application c#

I'm beginner in C# and I was trying to make WFA application. But there is one problem. When I run it, it shows first(login) form and if I close it without logging in, it will works fine, but when i login, first form hides, secound form shows up and if i close it by clicking X button, form disappeared but I can see process running in task manager. On secound form is also logout button, and if i click button it hides secound form, and shows first(login) form, and if i close it, still process running in background.
How can i stop process running in background when closing App by X button?
I was searching but i couldn't find solution for my problem. (i found something to attach to process and see threads but there is too many of them)
There is code where Forms are created hided etc:
If logged in frmLogin hides -> show frmMail
If logged out frmMail hides -> show frmLogin.
If user successfully logged in:
this.Hide();
frmMail f2 = new frmMail();
f2.Show();
If user click "Logout" button:
this.Hide();
frmLogin f1 = new frmLogin();
f1.Show();
I found solution, if everyone have same problem this may help.
How to switch between forms without creating new instance of forms?
How do I prevent the app from terminating when I close the startup form?
What I will do in your case is pretty simple.
Fist I will override the FormClose_Event, something like this
void Form_FormClosing(object sender, FormClosingEventArgs e)
{
//And here you can call
Application.Exit();
// which this will cause to close everything in your application
//Also if you want to be more aggressive, there is another option, you can
//use, Environment.Exit(1), which will basically kill you process.
}
I hope my answer helps you, have a nice day!

Minimizing a system windows form in tray in C# without seeing it hanging where taskbar

this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
//this.visible = false (Hotkeys stop working forever but the grey rest of the form would disappear from above taskbar)
ReRegisterHotkeys();
I use the code above to minimize my application with a tray icon. Now the minimized rest of my form hangs still in the left right corner a little above the taskbar where the start button resides. Visible is only the forms grey titlebar with the little "x" to close it and its caption text above the title bar.
This is very weird. I set my form to "mimimized" and set not to show in taskbar and it still does.
I have registered Hotkeys with my form so I may not set it to "invisible" else the hotkeys somehow cease to work even if I re-register the Hotkeys afterwards again.
I found no alternative yet to remove this minimized form caption other than set it to "invisible"
or removing its titlebar what I also don't want to do. I need titlebar, titlebar icon and titlebar control area in this program, the form shall not become a toolwindow or without borders.
How do I make this grey rest of the form above the taskbar disappearing without setting my form to a toolwindow and without setting it totally invisible. My Hotkeys must still work after it and the form must still keep its titlebar, icon and control area when I set it back to normal again.
I took my hotkey code from this example. The only difference is that I packed the procedure to register the hotkey into a sub-function named "ReRegisterHotkeys()".
Important:
The issue with the titlebar showing when form is minimized is not connected with the registered hotkeys.
It's a common "C# issue". If I have a form and minimize it and set it to invisible in taskbar
it is still shown the titlebar with the "x" in the taskbar. This I want to remove without making
the form invisible or without removing the window style.
"this.show" or "this.hide" behaves the same fatal as "this.visible=true/false" since the hotkeys are gone. I create my form as shown by default and want not to create it hidden already.
This is what shall be not there - how to remove it without hurting:
All you have to do is call Hide() and Show() when you want to hide and show your form.
NOTE: Hide() will hide from taskbar as well.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
Hide();
}
You may hide and show the NotifyIcon opposite to the form to not have a icon when the form is shown.
Obviously you need a NotifyIcon to display your app in the system tray.
Finally your code will look like this:
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}

Categories

Resources