Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow, i have proj1
i add 2 forms to Form1.cs(Designe) they are FormA and FormB
in FormA i add 2 buttons,they are

CallFormB_button and ExitFromFormA_Button

i added click to CallFormB_button so i can call FormB
it is CallFormB_buttonClick

in FormA, i added those codes
C#
protected override void OnClosing(CancelEventArgs e)
{
    this.Close();//<------
    //Base.Close();
}
private void ExitFromFormA_ButonClick(object sender, EventArgs e)
{
    this.Close();//,<-----
    //Base.Close();
}



in FormB i add 1 button it is

ExitFromFormB_Button

in FormB, i added those codes
C#
protected override void OnClosing(CancelEventArgs e)
        {
			this.Close();
			//Base.Close();
        }
        private void ExitFromFormB_ButonClick(object sender, EventArgs e)
        {
			this.Close();
			//base.Close();
        }

//---
i could find my way back from FormB and reach FormA
but i could not exit from FormA ! i want to reach Form1
the program hange and need Task manger to stop it
i tried base.Close() but no diffrence
Posted
Comments
CHill60 11-Jul-14 8:02am    
Why do you have "this.Close()" in the OnClosing event ... it's already closing.
Engineer khalid 11-Jul-14 8:53am    
few minutes ago i realize the above program works fine but acually my program looks like this

protected override void OnClosing(CancelEventArgs e)
{
DialogResult result;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if (result == DialogResult.Yes)
{
//base.Close();
this.Close();
}//Yes
else e.Cancel = true;
}
//------------------
private void ExitFromFormA_ButonClick(object sender, EventArgs e)
{
DialogResult result;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if (result == DialogResult.Yes)
{
//base.Close();
this.Close();
//Application.Exit();
}//Yes
;
}

protected override void OnClosing(CancelEventArgs e)
{
DialogResult result;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if (result == DialogResult.Yes)
{
//base.Close();
this.Close();
}//Yes
else e.Cancel = true;
}

private void ExitFromFormB_ButonClick(object sender, EventArgs e)
{
DialogResult result;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if (result == DialogResult.Yes)
{
//base.Close();
this.Close();
//Application.Exit();
}//Yes
;
}
Engineer khalid 11-Jul-14 9:05am    
your answer is correct you deserve the 5 points but i could not find any button to send this 5 points
CHill60 11-Jul-14 9:13am    
I've put that as a solution to wrap things up :)

1 solution

Problem was
C#
protected override void OnClosing(CancelEventArgs e)
        {
            this.Close();
            //Base.Close();
        }

Don't call the Close event when the form is already closing
 
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