Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# close one form from another

Posted on 2009-04-21
4
Medium Priority
?
2,824 Views
Last Modified: 2012-05-06
Hi

I am using c# with windows Forms.

I have a form that accepts some information, then opens a second form. After the second form is open I want to close the first form. How do I do that?
0
Comment
Question by:Charles Baldo
  • 2
4 Comments
 
LVL 13

Assisted Solution

by:joechina
joechina earned 200 total points
ID: 24195375
just close the form immediate after the code where you open the second form.
0
 

Author Comment

by:Charles Baldo
ID: 24195550
I Have this

       Success frm = new Success(CompanyID);        
       frm.ShowDialog();
       this.Close();

and it closes both forms.

 
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1800 total points
ID: 24195974
Right...if the first form is the "startup form" then you can't close it or the app will close!

One way around this is to start the app with an instance of ApplicationContext instead of an instance of a Form.  Then the app will not die unless you explicitly tell it to with Application.Exit().

Below is the implementation in "program.cs", Form1 and Form2:
(look closely for the breaks between the files)
namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyContext());
        }
 
        private class MyContext : System.Windows.Forms.ApplicationContext
        {
 
            public MyContext()
            {
                Form1 f1 = new Form1();
                f1.Show();
            }
        }
    }
}
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Close();
        }
    }
}
 
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Charles Baldo
ID: 31572793
Thanks both
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

810 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