How to programmatically close a dialog box from parent/owner control

S

Steve Sargent

Hi:

I'm writing an application that needs to be able to close a child
window when it receives an event from a server. The child window is
displayed as a dialog box.

Currently, I've tried using the Close() method on the dialog box from
the owner window, but it isn't having the effect I want. Basically,
the window freezes and I get an unhandled exception with the degugger
pointing at the ShowDialog() call.

<An unhandled exception of type 'System.ObjectDisposedException'
occurred in system.windows.forms.dll

Additional information: Cannot access a disposed object named
"GroupBox".>


If I let the dialog box close on it's own, It will be referencing
items that have already been disposed.

What is the proper way to programmatically kill a dialog box, if there
is one.


Thanks in advance for any help given.


Steve
 
N

Norman Yuan

in parent control, you do

MyDialog dlg=New MyDialog()
if (dlg.ShowDialog()==DialogResult.OK)
{
//Dlg is hide and allow you retrieve user's input on the dlg
//And do other thing s based on user input in dlg
}

//Dismiss dlg from memory
dlg.Dispose()

Key point is how the dialog box is closed.
You should close (actually hide) the dialog by setting its DialogResult
property to one of the DialogResult value. If you call this.Close() on
dialog's OK button, the dialog box is actually CLOSEd instead of hiding.
 
S

Steve Sargent

Thank you for the reply.

What I'm trying to do is to close the window before this statement.
The ShowDialog in the example below will block the current thread. I
need to programmatically close the dialog box from another thread
while the dialog box is displayed on the screen. (the dialog box is a
member variable of the class).


Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top