Solved

C# - Open/Close a form multiple times

Posted on 2013-02-05
5
1,169 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
[X]
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
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 86

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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

734 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