Reputation: 16842
My application's notification icon shows and hides the main application window accordingly. However, when a modal dialog is opened (this can be the About dialog or Settings dialog, to name a few) I want to the modal dialog to gain focus instead of showing/hiding the main application window.
The modal dialogs are opened with form.ShowDialog(this)
. Let's say I open the About window with that code and then, without closing it, I move to a different application. When I get back, if I click the notification icon, I want my application to get focus. Better yet, I want to the modal dialog to get focus cause since it's modal, I cannot use the parent form anyway.
I keep track with a simple bool
variable if any form (About, Settings, etc) is opened and when I click the notification icon I check for that variable. If it's true, I do something like mainForm.Activate()
. This actually brings the main form and the modal dialog to the front, the only problem is that it doesn't focus the modal dialog.
How can I solve this problem without keeping track of which modal dialog is opened and call .Activate()
on that? Cause that would be a pain...
Upvotes: 2
Views: 2480
Reputation: 16842
Like I said in the comments, my solution ended up using the Windows API.
Basically I keep track of any opened modal dialog with a bool
variable. When the notification icon is clicked, if the variable is false, it continues, if it's true, it calls the method below and stops code execution at that point (in the notification icon click event).
internal static void BringModalDialogToFront() {
mainForm.Activate();
SetForegroundWindow(GetWindow(mainForm.Handle, GW_ENABLEDPOPUP));
}
This easily solves my problem.
Upvotes: 1
Reputation: 1741
Do you get an Activate()
event in your modal dialog when the main window is activated? If so call SetFocus to the modal there.
If not, then when you get the Activate event in your main window, check if that (or any) dialog is open and call it's Activate or perhaps SetFocus directly if appropriate.
IOW, since you are getting the Activate event in the main window, propagate that to the modal dialog with a function call.
IMO, trying to get all this to work without direct intervention is going to be more of a pain than doing the work directly.
HTH
Upvotes: 1