Solved

C# -- Form2 "CANCEL-CLOSE" button

Posted on 2009-07-15
3
607 Views
Last Modified: 2013-12-17
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
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Pablissimo earned 500 total points
ID: 24862491
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
 
LVL 5

Expert Comment

by:Pablissimo
ID: 24862596
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
 

Author Comment

by:finance_teacher
ID: 24862657
Thanks for the GREAT help
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question