1

I like how MsgBox stops all further processing until it's clicked but I don't like how it pops that little msgbox in the middle of the screen.

Is there a way to do something like a msgbox but keep it on my Form (which is always in view) so I can go ahead and click a button on the form instead of having to bring the little msgbox window on top of windows that may be covering it.

I use the msgbox to inform me that a certain situation has happened, which I manually fix and when I'm done I click the MsgBox to continue processing. It'd be nice to have this button right on the form.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 1
    I don't really understand what you're asking. If you set your form as the owner of the messagebox, it will be displayed on top of your form. There's no reason it should ever be hidden behind anything else, or you would have to bring it to the front. I also don't understand if you want to be able to interact with the form while the messagebox is displayed. When you say "click a button on the form", what do you mean? – Cody Gray - on strike Jan 08 '11 at 07:48
  • @Cody - I edited the question a little for the first part of your comment !! – Gurucharan Balakuntla Maheshku Jan 08 '11 at 18:39

2 Answers2

3

which I then have bring to the front if there is a window covering it

That shouldn't happen, but can happen if you display the message box from a thread in your program. The window has the desktop as the parent and has no Z-order relationship with the windows in your user interface. And yes, can easily disappear behind the window of another app, including your own.

There's a MessageBoxOptions option that isn't exposed in Winforms, MB_TOPMOST, which ensures the window is top-most. You'd use it like this:

            MessageBox.Show("text", "caption", MessageBoxButtons.OK,
                MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                (MessageBoxOptions)0x40000);    // use MB_TOPMOST

But by far the best thing to do is to display the message box on your UI thread. Use Control.Invoke() to do so. That way the other windows of your app are disabled, no way for the user to not notice the box.

Still one problem with this, the user won't expect the box to show up since it is shown asynchronously from anything she does. Which means the box can easily get dismissed by accident when the user just happened to press the Enter or Space key. Or clicked at just the wrong spot. Nothing much you can do about that.

Centering the box in your main window is technically possible, but fugly to do. Check this answer.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Do you mean that the form shall exchange its contents with a message plus an OK button?

This would be similar to the way a text mode interface typically works.

You can make it happen by adding a disabled panel or UserControl with message and button topmost on the form and enable it when you wish to alert the user. However, I am puzzled how to implement the blocking behavior similar to MessageBox.Show() and Dialog.Show().

Christian Madsen
  • 1,688
  • 4
  • 17
  • 30