Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:00:50+00:00 2026-06-14T18:00:50+00:00

I am trying to understand the behavior of the following code in a WinForms

  • 0

I am trying to understand the behavior of the following code in a WinForms application.
I have two forms – Form1 and Form2. When i close Form1 from inside Form2, even Form2 gets closed. I have ensured that they both have the Owner property set to null.

Can anyone explain why Form2 gets closed as well from the code below?
Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

Form2:

public partial class Form2 : Form
{
    Form1 f1;
    public Form2(Form1 f1)
    {
        InitializeComponent();
        this.f1 = f1;
    }

    private void button1_Click(object sender, EventArgs e)
    {          
        f1.Close();         
    }
}

Program.cs

static void Main()
    {           
        Form1 f1 = new Form1();
        f1.Size = new Size(0, 0);
        f1.Show();           

        Form2 f2 = new Form2(f1);
        f2.ShowDialog();            

        Application.Run(new MainForm());
    }

When the Button on Form2 is clicked both Form1 and Form2 get closed. I have also ensured that the button is not the okbutton or cancelbutton for Form2. Can anyone explain why this happens?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:00:51+00:00Added an answer on June 14, 2026 at 6:00 pm

    Form1 closes because you have created a new Form1 and passed it to Form2, and inside Form2, you are closing the Form1. And just because Form2 is a DialogForm, and created after Form1, it is supposed as a DialogForm of Form1, so if Form1 is closed, the DialogForm is closed too. It is an expected behaviour rather than a quirky one.

    Either show it using

    f2.Show();
    

    or show the Form2 dialog form inside MainForm (by passing f1 to Form2 via MainForm), so Form1 closes but not Form2.

    Update:

    Try this in your Program.cs and you will see that Application’s MainWindow is closed after you close Form1.

    Form1 f1 = new Form1();
    f1.Size = new Size(0, 0);
    f1.Show();
    
    MessageBox.Show(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToString());
    
    Form f2 = new Form2(f1);
    
    MessageBox.Show(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToString());
    
    f2.ShowDialog();
    
    MessageBox.Show(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToString());
    
    Application.Run(new MainForm());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the behavior of vector::pop_back() . So I have following
I'm trying to understand an estrange behavior in Python. I have the next python
I am trying to understand the following bit of code: #include<iostream> using namespace std;
I'm trying to understand the behavior of exceptions in c++. I wrote the following
I am trying to understand the following behavior of shared libraries in C Machine
I have the following program in which I'm trying to understand the functioning of
I'm trying to understand the behavior of Scala for-loop implicit box/unboxing of numerical types.
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand Ruby a bit better, I ran into this code surfing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.