views:

111

answers:

2

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

+1  A: 

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

JustBoo
The problem is that some of these modal dialogs are not exactly Forms as the About/Settings one. A few of them are Task Dialogs from a library I'm using, which I believe uses the new Task Dialogs API introduced in Vista. Those don't have an Activate event.
Nazgulled
Do the Task Dialogs have a public SetFocus() function? If not, could you set focus to a control, a button, a textbox, any kind of control that takes focus that is in the dialog? I think you can get a control by ordinal number; the "first control", so to speak, so it will be a generic solution. That is, not tied to a particular dialog.
JustBoo
Unfortunately no, nor the Task Dialog nor any control takes focus :( But I think I fixed my problem by resorting to the Windows API, more specifically using `SetForegroundWindow()` and `GetWindow()`.
Nazgulled
+1  A: 

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.

Nazgulled