compactframework >> Need to close dialog form in load event

by PeterB » Fri, 22 Jul 2005 16:12:00 GMT

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



compactframework >> Need to close dialog form in load event

by PeterB » Fri, 22 Jul 2005 16:17:16 GMT


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


"PeterB" < XXXX@XXXXX.COM > skrev i meddelandet

compactframework >> Need to close dialog form in load event

by Christian Schwarz » Fri, 22 Jul 2005 21:44:11 GMT

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

compactframework >> Need to close dialog form in load event

by PeterB » Fri, 22 Jul 2005 23:18:29 GMT

Hi Christian

Thanks for you suggestion, I will try it a.s.a.p.

/ Peter


"Christian Schwarz" < XXXX@XXXXX.COM >

Similar Threads

1. Form.Closing event not fired always when i call Form.Close method - .NET Windows Forms

2. Closing a form on the Load event?

Under a certain circumstance, I want a form to display a messagebox in the
load event, and then close. But apparently I cant close a form in the load
event. Any ideas?

Thanks




3. Form: calling Close() method in Load event. - VB.Net

4. Don't need to do load event from inheritance form

Hi all,

Assuming I have 2 form controls on my application. First form is parent form 
and second form is inheritance form that inherit from first form.

When I want to view designer of second form (inheritance form) it is always 
done at load event of first form (parent form). I have test by set form 
visible property at load event of first form to false and then when I load 
the second form the form is invisible. Can anyone explains to me?

I don't need to do load event of first form because sometimes I want to add 
new control to second form but it occurs some error. How can I ignore the 
load event of parent form?


Thanks,
KPH



5. Need to launch event after form load - CSharp/C#

6. Need some info about the Load event of Forms and Usercontrols

Threre is a Form containing a usercontrol

In the form's Load event it references a usercontrol property, say, zz

The first showdialog(formx) causes
1 usercontrol_load event
2 form_load event which causes zz as expected

the second showdialog(formx) causes
1 form_load event which causes zz as expected
2 usercontrol_load event


Note in the second situation zz ran before the Load which initializes 
things.

I would like the usercontrol load event to run before any properties or 
methods are executed. Is that possible to setup.



What are the sequences that form and usercontrol loads are run?

Do they always both run for each DialogShow?


What happens after the DialogShow exits? Something is left in memory, but 
what? The Usercontrol? The Form? Parts of each?

The usercontrol contains a TreeView, is any of the treeview data preserved?



Thanks for any answers



7. form closed & closing event - .NET Windows Forms

8. BUG: form closed & closing event

I am having a problem with raising the Form.Close and 
Form.Closing event. This only occurs if I don't call 
Form.Show() before a call to Form.Close(). Is this by 
design?

MyForm f = new MyForm();
f.MdiParent = this;
if (f.SomeFunction() == false)
 f.Close(); <--- Closed event does not fire
else
 f.Show();

...

if (f!=null)
 f.Close(); <--- Closed event fires


MyForm is derived from Form and subscribes to its 
Form.Closed event.

I am running 1.1 of the framework.

Nick