Mehbube Arman
Mehbube Arman

Reputation: 490

Is It Possible that An Object is Disposed But not NULL

In one of my code, I used a public static object of a form. In this code I have used Show() and Hide() function on this form, because I don't want to close this form as long as the main application is running. Now if I close the form from "Task manager - > Application Tab" this form gets disposed. I have function like the following:

public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
if(fullScreen == null)
{
fullScreen = new fullScreen();
}

return fullScreen;
}

Now when I call "GetBackFullScreen().Show()", I get can not show Disposed Object form. Can anybody suggest a solution? Thanks in advance.

Upvotes: 2

Views: 1348

Answers (2)

Lukazoid
Lukazoid

Reputation: 19416

I do not entirely understand the problem here, but maybe you could try the following:

public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
    if(fullScreen == null)
    {
        fullScreen = new fullScreen();
        fullScreen.Closed += (s, e) => fullScreen = null;
    }
    return fullScreen;
}

This will ensure that whenever the form is closed, the backing field is cleared, and thus a new form is subsequently created.

Upvotes: 0

Oscar
Oscar

Reputation: 13960

public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
if(fullScreen == null || fullScreen.IsDisposed)
{
    fullScreen = new fullScreen();
}

return fullScreen;
}

Get if the form is disposed, if so, create a new instance.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isdisposed.aspx

Upvotes: 3

Related Questions