Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# - Open/Close a form multiple times

Posted on 2013-02-05
5
1,143 Views
Last Modified: 2013-02-05
Good morning,

New to C#. I've created a small program with an "about" menu item. When clicked the program opens another form. I want to code this so you can close and open this about forms as many times as one wishes. The way I have it coded, the about form will open and close once, but when you try and open it again it throws an error ("Can't access a disposed object").

As I am teaching myself, I feel a bit lost. What is the best way to open and close an additional form from some event in the main form as many times as one wishes?

A sample of the code I'm using:

private about af = new about();

private void dispAbout(object sender, EventArgs e)
{
af.StartPosition = FormStartPosition.CenterScreen;
af.MinimumSize = new Size(600,300);
af.show();
}
0
Comment
Question by:ThePATMAN26
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38855741
get rid of the private about af member and use this:
private void dispAbout(object sender, EventArgs e)
{
about af = new about();

af.StartPosition = FormStartPosition.CenterScreen;
af.MinimumSize = new Size(600,300);
af.show();
}

Open in new window

0
 

Author Comment

by:ThePATMAN26
ID: 38855779
Ah, I knew I was close to the mark. Didn't even think to give that a shot. Thank you very much.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38855784
The problem is that once you hit close (eg. the x at top right on the form) then it destroys the form - hence your error when you attempt to show it again.
(You want the form to be hidden not closed.)


Is there a particular reason you want to keep the form - user input for example.  If it is just to display the same information then don't try to keep the form, just create a new one as necessary.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38855793
ps.  The answer you accepted (whilst I was typing my comment) is only suitable in some circumstances.

eg.  Without closing this new form try to open it again a few times - is that what you want ?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38855882
I'd have to agree with Andy.  You probably didn't want multiple instances of your about available at the same time did you?

In addition to sedgwick's suggestion, I'd change Show() to ShowDialog():

    af.ShowDialog();

With your original approach, though, you can use a check like this if you need to leave the declaration of the form out at class level (which is desirable under certain circumstances):
        private about af = null;

        private void dispAbout(object sender, EventArgs e)
        {
            if (af == null || af.IsDisposed)
            {
                af = new about();
            }
            af.StartPosition = FormStartPosition.CenterScreen;
            af.MinimumSize = new Size(600, 300);
            af.Show();
        }

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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