Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All ;
My application is going strange ...
before I was making the form border style = none then I create my own close button which will close the curent window and open and pass values to other form. and was working prefectly.. for example if I have form1 and I want to close it and open form2 and after finishing I want to open again form1 after closing form2...

My code was like that :

openform2(object sender, EventArgs e)
    {
        functions fn = new functions();
        form2 ts = new form2();
        ts.Text = " Welcome Mr." + fn.GetuserNameFromFile();
        this.Hide();

        ts.ShowDialog();
        this.Close();
    }
then I opens form2 and so on...


after that I make the formborderstyle = sizable then I want to catch the form close event without using my own button .. what I have done in the form2 is the following :
at the intialize function I have added this line :

MIDL
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);


and then I carete the function :

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        functions fn = new functions();
        form1 f1 = new form1();
        f1.Text = " Welcome Mr." + fn.GetuserNameFromFile();
      // that means to close the current form2 and show form1 again 
        this.Hide();
        f1.ShowDialog();
        
        this.Close();
        this.Dispose();
    }



so what happens now the form2 closed succesfully , but when I try to close the form1 after finishing my proccess so I click on close button it is hiding it and then showing it hidding and showing ... so it is not closing ...

how to kill the proccess I want to close the software...

help...
regards..
Posted

You are doing this.Close() in the FormClosing event. That is pretty strange considering that the form is already trying to close.

Replace this:
C#
this.Close();
this.Dispose();


With this
C#
e.Cancel = false;


Good luck!
 
Share this answer
 
Comments
Tamer Hatoum 15-Dec-10 12:17pm    
If I will put e.canecl is that means that I close the current event which is trying to close the window?? No just I was thinking that when I call the formclosing function it will not close the window it will just apply the code inside the function, so for that I have add this.close, but now I commented all the code and make it emptye function and it closes the window .. so by conclusion it is closing the windows and when we are adding : += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
that means we are adding to main event a new functionality..
that is great.. thank all for you comment
Hi ,

Remove

this.
MIDL
Close();
        this.Dispose();


Form your code. On execution of this line form closing event occur.
 
Share this answer
 

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