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 8745765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:05:19+00:00 2026-06-13T12:05:19+00:00

I have 2 Forms in my app, Form1 and Form2 . I want to

  • 0

I have 2 Forms in my app, Form1 and Form2. I want to know if there is a
better way of checking if the form has already been loaded. If the form is
loaded in memory and visible, I want the button to do nothing, if the User
has closed the form it should re-instantiate it.

For more info check my code or comment.. 🙂

public partial class Form1: Form
{
    private Form2 form2;

    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {

        if (form2== null) {
            form2= new Form2();
        } else {
            if (form2.has_exited()) { // this is a private property
                                      // that I set when Form2.Closing executes
                form2.Dispose();
                form2= new Form2();
            }
        }
        form2.Show();
    }
}
  • 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-13T12:05:20+00:00Added an answer on June 13, 2026 at 12:05 pm

    You don’t need to have Form2 handle the closing event, you can just do it directly from Form1:

    private Form2 form2;
    
    private void button1_Click(object sender, EventArgs e)
    {
        if (form2 == null)
        {
            form2 = new Form2();
            form2.FormClosed += (_, arg) =>
            {
                form2 = null;
            };
            form2.Show();
        }
        else
        {
            //the other form has been opened and not closed; not sure what you want to do
        }
    }
    

    Also note that if a form is shown via Show you don’t need to dispose of it. If you show it via ShowDialog then you do, but when you use the non-modal dialog the system will automatically attach an event handler to the closing event that calls dispose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WinForms app and multiple forms and want to use ErrorProvider component on
I have a form in google app engine where I want to upload an
In a Windows Forms app is there any control that allow to have a
I have a Windows Forms app in C#/.NET4 with a SQL 2008 back end.
I have a Windows Forms app written in C#, and part of the app
I have a windows forms app where I have split different functionality into several
I am working on a fairly complicated project. We have a c# forms app
I am building multiple forms app in Builder XE2 and I have a problem
I have an embedded database in windows forms app. Is it possible to browse
I have installed a C# windows forms app on a client machine which does

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.