Link to home
Start Free TrialLog in
Avatar of finance_teacher
finance_teacher

asked on

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) ?
        }
ASKER CERTIFIED SOLUTION
Avatar of Pablissimo
Pablissimo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pablissimo
Pablissimo

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.
Avatar of finance_teacher

ASKER

Thanks for the GREAT help