Solved

C# - Open/Close a form multiple times

Posted on 2013-02-05
5
1,134 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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