471,319 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Static Form, that work like MessageBox?

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.

Dec 14 '05 #1
4 9566
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Bry" <br*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.

Dec 14 '05 #2
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.

"Bry" <br*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.

Dec 14 '05 #3
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.

Dec 14 '05 #4
Bry
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.

Dec 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by .pd. | last post: by
1 post views Thread by Kevin Carter | last post: by
6 posts views Thread by Goran Djuranovic | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.