2

I have a large application with several forms, any of them could get a MessageBox (MessageBox.Show()) that is modal and locks the form.

On activation of another form I now need to find this MessageBox and bring the form that has this MessageBox to front. Is there any way to check this?

I know about the Application.OpenForms property, maybe there is something like this for MessageBox?

Edit1 : For example, say that we open Winform1, then a event in Winform1 will go to the mainController that opens Winform2. Lateron Winform1 is getting a MessageBox.Show, But its fully possible to bring Winform2 to front(above Winform1). So now I need to react to the Winform.Activated to check if there is any MessageBox.Show and if so, bring this form that holds the MessageBox to front.

Banshee
  • 15,376
  • 38
  • 128
  • 219
  • 4
    I'm confused. If the message box is modal and open, then no other forms should be opening or activating... – Merlyn Morgan-Graham Nov 17 '11 at 08:36
  • @MerlynMorgan-Graham: The first locked form is surely not the only way to open forms from his application. – Otiel Nov 17 '11 at 08:38
  • @Otiel: Unless something in the background is popping up forms, then *all* forms should be locked, and thus unable to pop up forms in response to the user. I thought that's what modal meant - all forms are locked. – Merlyn Morgan-Graham Nov 17 '11 at 08:39
  • @MerlynMorgan-Graham: Not necessarily. If forms are opened from a systray menu for instance, opening a `MessageBox` from a form does not block the opening of another form. Though, "*bring the form that has this MessageBox to front*" will certainly be impossible. – Otiel Nov 17 '11 at 09:01
  • You might be able to find the parent of the active form (the form owning the messagebox), and set your new form in z-order behind that form. Not sure how much of a pain this is though, or how possible it is. – Merlyn Morgan-Graham Nov 17 '11 at 09:08

2 Answers2

0

You could find them by using Application.OpenForms like this:

foreach (Form f in Application.OpenForms)
{
    if (f.Visible && ! f.CanFocus)
    {
        // whatever...
    }
}

Or: use a different approach altogether:

Make all your forms handle Application.EnterThreadModal and Application.LeaveThreadModal, so that when the app goes modal while that form is current, you add that form onto a list so you can keep track of it, and remove it from the list when it leaves modal...

Then all you need to do is query that list to see if any forms have a modal dialog box open.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
andyhasit
  • 14,137
  • 7
  • 49
  • 51
  • 1
    This does not hold the forms created for MessageBox.Show? I have checked and my Application.OpenForms.Count is the same irrespective of if the MessageBox is shown or not. – Banshee Nov 17 '11 at 09:44
  • Aha, it seems that Application.OpenForms isn't reliable (It shows 0 for me on my current app even though the main form's showing) look here for details of bug: http://stackoverflow.com/questions/3751554/application-openforms-count-0-always Perhaps if you could cycle through the forms in a different way, then use (f.Visible && ! f.CanFocus)...? – andyhasit Nov 17 '11 at 10:15
  • Did you have any luck with the second approach I suggested in my edit? – andyhasit Nov 17 '11 at 13:09
  • I will probably switch all MessageBox.Show to my own dialog handler that opens a small winform in dialog form. This will make the Appliacation.OpenForms work as long as the bug with the Application.OpenForms do not come in to play. – Banshee Nov 17 '11 at 14:40
  • Pushing all messages displays through your own function is clearly the easiest way as you can then track dialog boxes yourself without relying on Application.OpenForms. I just assumed you wanted to avoid this solution - in case a MessageBox.Show() accidentally appears somewhere due to forgetfulness or someone else changing your code... Anyway, you now have a range of options. All the best. – andyhasit Nov 17 '11 at 15:22
0

Try using one of the Show methods that takes an owner:

MessageBox.Show(this, "My Message");

I tested this on .NET 4 / Windows 7, and when the message box is opened it brings its owner to the front.

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127