How about:

using (MyDialog dialog = new MyDialog ())
{
        // Call your initialise method
        dialog.MyInitialise ();
        if (dialog.ShowDialog () == DialogResult.OK)
        {
                // Fetch my results
                results = dialog.GetMyResults ();
        }
}

So, you can add whatever initialisation you need in your MyInitialise ()
method, and get whatever values you need from your GetMyResults () method.

The 'using' statement will handle the Disposal automatically, and you're
instantiating a new MyDialog each time through the code so you shouldn't
have any problems with re-using an existing object.

As for the event that's fired every time ShowDialog is called, the only way
I've found of doing this is putting code in OnVisibleChanged, and checking
whether the form is visible or not.  I didn't really like that...

Hope this helps,

                        Geoff

P.S. that's all uncompiled 'air code', so there may be typos etc.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Eddie Lascu
Sent: 27 November 2006 15:39
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Events fired when Form.ShowDialog is called

Hello everybody,

The MSDN says about ShowDialog that

"When a form is displayed as a modal dialog box, clicking the Close button
(the button with an X at the upper-right corner of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form button of a dialog box or sets the value
of the DialogResult property. Instead the form is hidden and can be shown
again without creating a new instance of the dialog box. Because a form
displayed as a dialog box is not closed, you must call the Dispose method of
the form when the form is no longer needed by your application."

The way I understand this is that even if I re-instantiate the class that
implements my form, the framework will reuse some previously created
structures and that's how I would explain why the Load event is not fired
for subsequent calls to ShowDialog.

So here is my question: What is the pattern I need to use to display one
form over and over and make sure that all internal controls are initialized
to proper values every time the form is shown? What is the event that's
fired every time ShowDialog is called, if there is such an event? If not,
how else should I approach this?

TIA,
Eddie

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to