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

  • Home
  • SEARCH
  • 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 165261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:53:23+00:00 2026-05-11T11:53:23+00:00

I have a form which will open a new form when one button (form1button)

  • 0

I have a form which will open a new form when one button (form1button) is clicked. And on the child form there will be another button ‘form2button’. Now if I click this form2button the new form2 should be disposed. But because the form2 object is created here in form1 class method, I cannot dispose that object in form2 class method (fom2buttonclick). So I used static to get my work done as in the following psuedo code.

Form1:

class Form1 : Form {     static Form2 f2;      public void Form1_buttonclick(object sender, EventArgs e)     {         f2 = new Form2();     }      public void Disposef2()     {         f2.Dispose();     } } 

Form2:

class Form2 : Form {     public void Form2_buttonclick(object sender, EventArgs e)     {         Form1 f1 = new Form1();         f1.Disposef2();     } } 

Is there any other better way to do it. Or C# design itself doesnot provide an alternative mechanism. I am new to C#.Please help me out..

Edit

I want to close (dispose explicitely) form2 object which is created in form1 class when button on form2 is clicked. This edit is to give some more clarity.

  • 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. 2026-05-11T11:53:24+00:00Added an answer on May 11, 2026 at 11:53 am

    If the two forms doesn’t have a parent-dialog type of relationship, you might just want to hook into the Disposed event on the subform to get notified when it closes.

    public partial class Form1 : Form {     private Form2 _Form2;      public Form1()     {         InitializeComponent();     }      private void button1_Click(object sender, EventArgs e)     {         if (_Form2 != null)             _Form2.Dispose();          _Form2 = new Form2();         _Form2.Disposed += delegate         {             _Form2.Dispose();             _Form2 = null;         };         _Form2.Show();     } } 

    Then all you have to do in Form2 is simply to close it:

    public partial class Form2 : Form {     public Form2()     {         InitializeComponent();     }      private void button1_Click(object sender, EventArgs e)     {         Close();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 196k
  • Answers 196k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've used both of those plugins. I used minibuf explorer… May 12, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer Here's a blog post that answers your question: iBatis: Support… May 12, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer Ok not a case of staring at my computer too… May 12, 2026 at 7:08 pm

Related Questions

So I would consider myself a .Net and ASP.NET pro but I am a
In my ASP.Net application I have a requirement that when a user clicks on
I want to prevent the user from maximizing the Windows Form to full screen
I'm trying to make some ajax-functionality in my web application, but I cannot get

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.