Solved

C# -- Form2 "CANCEL-CLOSE" button

Posted on 2009-07-15
3
592 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
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Pablissimo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the GREAT help
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now