Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,

Please look at the code below. I'm showing a form on the screen and then I would like to wait for the form to close before I continue with the rest of the code. Note that I cannot do ShowDialog (this a requirement) on the form. So I call Show method on the form.

private void button1_Click( object sender, EventArgs e )
{
  Form f = new Form( );
  f.FormClosed += new FormClosedEventHandler(f_FormClosed);
  f.Show( );
  // Here I want to wait for this form
  // to close before continuing with the rest of the code

  // Once closed, I would like to go on with the rest of code here.
  //For example:
  Console.WriteLine("My waiting is done.");

}

void f_FormClosed( object sender, FormClosedEventArgs e )
{

}


How do I go about achiving this?

Many thanks in advance.
/Mizan
Posted
Updated 8-Aug-12 0:32am
v3
Comments
Prabhakaran Soundarapandian 8-Aug-12 6:37am    
is this silverlight project?

Use onCLosing as this is triggered before the Close event is, you could put your code in there?
 
Share this answer
 
v2
C#
private void button1_Click( object sender, EventArgs e )
   {
     Form f = new Form( );
     f.FormClosed += new FormClosedEventHandler(f_FormClosed);
    f.FormClosing += new FormClosingEventHandler(f_FormClosesing);
    f.Show( );


  

  }

 f_FormClosesing(object sender, FormClosingEventArgs e e)
{
 //  Write Here code which you want to run before form close;
}

  void f_FormClosed( object sender, FormClosedEventArgs e )
  {
  // your code when form closed
    Console.WriteLine("My waiting is done.");
  }
 
Share this answer
 
v2
If it is a modal dialog, call ShowDialog on that Form (e.g. see http://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.80).aspx[^]).

If you insist on working with events, see http://msdn.microsoft.com/en-us/library/ms173179(v=vs.80).aspx[^].

Cheers
Andi

PS: *Why* is it a requirement to not call ShowDialog()? You might need to elaborate on that on what issue requires you to not use that very convenient and obvious solution. Is the one requiring you to not use ShowDialog() willing to pay the cost (re-inventing the wheel, development time, testing cost, etc.)?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900