tags:

views:

3053

answers:

6

Why is C#.Net message box not modal? Accidently, if the message box goes behind our main UI, then main UI doesn't respond, until we click Ok (on our message box).

Any workaround other than creating custom message box?

Thanks in advance.

+3  A: 

A modal pop-up is technically defined as a pop-up box that interrupts the normal flow of the application...not necessarily one that stays on the top of all other windows so the behavior you're describing is correct for a modal popup.

Modal Window

Here's a project on CodeProject that tries to mimic the "always on top" functionality for a MessageBox style Modal window:

CodeProject: TopMost MessageBox

Justin Niessner
His problem isn't that the dialog box gets covered by a different app. It's getting covered by the calling app. This leads to an app that cannot be used, because the dialog cannot be closed. The dialog cannot be closed, because it cannot be accessed.
Brad Bruce
Good point...I was going with this approach because it would help either situation, but if he's not concerned with other apps then setting the owner is the way to go.
Justin Niessner
+18  A: 

You need to assign the MessageBox owner property to the main UI window (look at the 3rd constructor).

Charlie
+2  A: 

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, to place the message box in front of. A message box is a modal dialog, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

http://msdn.microsoft.com/en-us/library/aa335423(VS.71).aspx

Dusty
+2  A: 

This is a simple C# New Winform App >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message = "You did not enter a server name. Cancel this operation?";
            string caption = "No Server Name Specified";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            // Displays the MessageBox.

            result = MessageBox.Show(this, message, caption, buttons);

            if (result == DialogResult.Yes)
            {

                // Closes the parent form.

                this.Close();

            }


        }
    }
}

As Dusty states above a message box is a modal dialog. Specify the 'owner' property. << in this example the owner denoted by the keyword 'this'.

JamesM
+1 for the shout out. :)
Dusty
A: 
public static System.Windows.Forms.DialogResult WW_MessageBox(string Message, string Caption,
        System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon,
        System.Windows.Forms.MessageBoxDefaultButton defaultButton)
    {
        System.Windows.Forms.MessageBox.Show(Message, Caption, buttons, icon, defaultButton,
            (System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);

    }
A: 

Make the message box appear in the main thread, if your form has been created from it :

private bool ShowMessageBoxYesNo()
{
    if (this.InvokeRequired)
     return (bool)this.Invoke(new ShowMessageBoxYesNoDelegate(ShowMessageBoxYesNo));
    else
    {
     DialogResult res = MessageBox.Show("What ?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
     if (res == DialogResult.Yes)
      return true;
     else 
      return false;
    }
}
Max