Need to close dialog form in load event

P

PeterB

Hello!

Using C# on WM2003SE. I have a parent form which opens a child form with
ShowDialog(). When exiting the child form I usually just set DialogResult to
a value and execution is transferred to the parent again.

If I am doing some checking in the constructor or load event of the child
form, and the result of this check is that I need to go back to the parent
form, it's not enough to just set the DialogResult. Instead I have to
explicitly call this.Close() in the constructor or load event (and ofcourse
set the DialogResult to an appropriate value).

Could someone explain why this is so?

Thanks,

Peter
 
P

PeterB

Oh... calling this.Close() in the constructor is not very wise, you'd have
to do the checking and closing in the load event!

/ Peter
 
C

Christian Schwarz

Peter,

maybe throwing an exception from within the constructor or the OnLoad()
method could be a solution to your problem.

....
MyForm myForm = null;
try
{
myForm = new MyForm();
DialogResult result = myForm.ShowDialog();
...
}
catch (Exception ex)
{
...
}
finally
{
if (myForm != null)
myForm.Dispose();
}
....

public class MyForm : Form
{
...
public MyForm()
{
if (...)
throw new ApplicationException(...);
}
...
protected void override OnLoad( don't remember the args )
{
if (...)
throw new ApplicationException(...);
}
...
}

Greetings, Christian
 

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