Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Static Form, that work like MessageBox?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bry - 14 Dec 2005 18:02 GMT
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.
Nicholas Paldino [.NET/C# MVP] - 14 Dec 2005 18:12 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Is it possible to produce a Windows Dialog form that can be used like
> MessageBox.ShowDialog(), but returns a custom object type
[quoted text clipped - 13 lines]
>
> Thanks.
Marina - 14 Dec 2005 18:16 GMT
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.

> Is it possible to produce a Windows Dialog form that can be used like
> MessageBox.ShowDialog(), but returns a custom object type
[quoted text clipped - 13 lines]
>
> Thanks.
daniel.manges@gmail.com - 14 Dec 2005 20:09 GMT
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.
Bry - 14 Dec 2005 21:47 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.