Solved

C# - Open/Close a form multiple times

Posted on 2013-02-05
5
1,098 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now