Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

C# -- Form2 "CANCEL-CLOSE" button

How can I get my "Form2" CANCEL button to close the form without making user's click the upper right "X" ?
--------------------------------------------
Example
 1. create Form1 & Form2
 2. add below code to Form1 "Open Form2" button
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
        }
 3. add below code to Form2 "Cancel" button
        private void button2_Click(object sender, EventArgs e)
        {
            Form2..... (cancel or close) ?
        }
0
finance_teacher
Asked:
finance_teacher
  • 2
1 Solution
 
PablissimoCommented:
Might have misunderstood the question, but the following for Form2's Cancel button event handler should do it:

private void button2_Click(object sender, EventArgs e)
{
  this.Close();
}

Or do you want to close Form2 from a button click on Form1?
0
 
PablissimoCommented:
As an aside, if you want to force the user to accept or cancel whatever's on Form2 before they return to Form1, try changing the

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
        }

method in Form1 to be

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.ShowDialog(); // Note ShowDialog instead of Show
        }

ShowDialog'll wait until the Form created is closed.
0
 
finance_teacherAuthor Commented:
Thanks for the GREAT help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now