1

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
  • Why do you need this? It seems like you can just replace "msg" with System.Windows.Forms.MessageBox, and are passing the parameter around for no real gain. If this is so you can abstract messages, and have some go to boxes and some elsewhere, just implement an "elsewhere" that invokes MessageBox Sep 24, 2008 at 18:45
  • This is obviously a simplification of my problem. I will eventually be implementing my own itnerface, but for now would like something quick and dirty for a prototype Sep 24, 2008 at 19:05

6 Answers 6

2

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

0
2

Looking at the comments. Encapsulation is your answer :)

0

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

0

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.

0

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.

0

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.

2
  • 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. Feb 27, 2009 at 15:23
  • Cool, hadn't noticed the date. Feb 27, 2009 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.