tags:

views:

305

answers:

6

I would like something that I can use as follows

var msg = new NonStaticMessageBox();
if(msg.Show("MyMessage", "MyCaption", MessageBoxButtons.OkCancel) == DialogResult.Ok)
 {....}

But specifically non-static (I need to pass a reference to it around) does anyone know if/where such an object exists?

+2  A: 

Such an object does not exist in the .net framework. You'll need to roll your own.

Jekke
Wow, thats an unfortunate oversight, ok, thanks.
George Mauer
+2  A: 

Looking at the comments. Encapsulation is your answer :)

leppie
A: 

why do you need to pass a reference of it? you could just use MessageBox.Show and that's all? if you really need it you could make your own MessageBox class, something like:

public class MessageBox
{
    private Form _messageForm = null;

    public void Show(string title,...) {...}
}

or you could inherit MessageBox class and implement your own instance members... however I don't see any sense in this...

A: 

Bear in mind that, at the end of the day, the S.W.F.MessageBox.Show() methods are all basically wrappers around the core Win32 MessageBox() API call. (Run mscorlib through Reflector; you'll see the "real" code in the private methods called ShowCore.)

There is no provision (as far as I know) for caching the called MessageBox in Win32, therefore there is no way to do so in .NET.

I do have my own custom-built MessageBox class which I use -- although I did so not to cache it (in my usage scenarios in WinForms, the same MB is rarely used twice), but rather to provide a more detailed error message and information -- a header, a description, an ability to copy the message to the clipboard (it's usually the tool which notifies the user of an unhandled exception) and then the buttons.

Your mileage may vary.

John Rudy
A: 

You might want to have a look at the ExceptionMessageBox class that comes with SQL Server. It is in a self-contained assembly, but I'm not sure if you are allowed to redistribute it without SQL Server - you might need to check on this.

Christian.K
A: 

You say

"This is obviously a simplification of my problem."

However your question doesn't reveal a problem we can solve without more information about intent.

Given that any form can be shown modally by calling ShowDialog and in the form returning DialogResult. I'm not seeing an issue here. You can pass whatever parameters you like into it, define the contents as you like, then call:

MyFactory.GetMyCustomDialogWithInterfacesOrSomesuch myDialog = new ...
myDialog.ShowDialog() == DialogResult.Ok;

Because you're dealing with form and not MessageBox, it's not static so it's not an issue.

TreeUK
This was from a long time ago and the solution was indeed to create my own messagebox for a form. One reason to have it be an instance rather than static method is to allow for unit testing, though in my case I also wanted to be easily able to switch in implementations.
George Mauer
Cool, hadn't noticed the date.
TreeUK