Static Form, that work like MessageBox?

  • Thread starter Thread starter Bry
  • Start date Start date
B

Bry

Is it possible to produce a Windows Dialog form that can be used like
MessageBox.ShowDialog(), but returns a custom object type

e.g.

myObject = MyForm.ShowDialog();

rather than

// This method doesn't return the object either and ShowDialog can't be
// overriden to make it behave like that
MyForm frmMyForm = new MyForm();
frmMyForm.ShowDialog();

Can anyone suggest any soloution to this?

Thanks.
 
Bry,

MessageBox does not have a ShowDialog method. It has a Show method
which is not related to ShowDialog at all. You can create your own class
with a static method and have it return whatever you want.
 
The MessageBox class is not a windows form or a dialog. It's just a regular
class that happens to have a static Show method. This method instantiates a
new form object, displays it, returns the result.

So you can very easily write the same thing.
 
If you want it to work exactly that way, where the static method you
call returns an object, the only way I can think of to do it is to use
a class inbetween the main form and the dialog box.

Code for main form:

object test = TestClass.Show();
MessageBox.Show(test.ToString());

-----------------------------------------------------------------
class TestClass
{
private object whatever;
private static TestClass instance;
protected TestClass() {}
public static object Show()
{
instance = new TestClass();
TestForm tf = new TestForm();
tf.FormClosing += new FormClosingEventHandler(tf_FormClosing);
tf.ShowDialog(); //must use show dialog so the return waits for
the form to close
return instance.whatever;
}

private static void tf_FormClosing(object sender,
FormClosingEventArgs e)
{
instance.whatever = ((TestForm)sender).whatever2;
}
}

-------------------------------
TestForm

The TestForm in this example would just have to have an instance
variable called whatever2. Hopefully this gives you a general idea of
how you could do it. You have to use .ShowDialog() on the test form so
the return line waits for the form to close to do the return.
 
Many thanks to all for the replies. I was under the impression the
MessageBox class was a windows form class.

Now that I've got that cleared up, I've managed to do what I was trying
to do.

Thanks.
 
Back
Top