Need to close dialog form in load event

  • Thread starter Thread starter PeterB
  • Start date Start date
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
 
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
 
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
 
Back
Top