Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

Why cannot access disposed object message when closing a window.

I have a utility window that I open in a WFP C# project. One of the options allows a user to transfer a PO. To do that the user needs to enter a PO number. So I pop open another window. On this window I allow them to enter the PO number. I put an Exit button on that window. For some reason I cannot explain, when they click on the Exit button I get "Cannot access a dispose object" message. I am calling this window with the code below. The Exit button code to exit is below that.

Call window with this:

private void btnTransferPO_Click(object sender, EventArgs e)
{
    string TransferText = "Purchase Order";
    using (frmTransferWindow TransferWindowForm = new frmTransferWindow(TransferText))
    {
        if (TransferWindowForm.ShowDialog() == DialogResult.OK)
        {
            TransferWindowForm.Show();
            TransferWindowForm.Activate();
            TransferWindowForm.Focus();
        }
    }
}

Open in new window


Exit button has this code:

private void btnExit_Click(object sender, EventArgs e)
{
    this.Dispose();
    this.Hide();
}

Open in new window


What am I missing here? I have hundreds of other Exit that I close this way with no issues.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi rwheeler23;

When you enter the function btnTransferPO_Click it creates a new frmTransferWindow. When the code leaves that function it automatically closes then frmTransferWindow because it is defined using the using statement.

Fernando
Avatar of rwheeler23

ASKER

Calling the form this way results in the same error message. What is the proper way to call the form so that when I exit I do not get this message? There is one button to transfer the PO and another to exit, in case the user wants to back out without doing anything. It is the Exit button causing this issue.

            TransferWindowForm = new frmTransferWindow(TransferText);

            TransferWindowForm.Show();
            TransferWindowForm.Activate();
            TransferWindowForm.Focus();
ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you cannot hide something that has been disposed!
This one has me stumped. In this case I open a window(A) that contains 5 buttons. Under two of these buttons is the window(B) call I mentioned earlier. On that window is an Exit button. Under the exit button is Hide() and Dispose(). It is this button that creates the error message. I have the same Hide() and Dispose() on the calling window and I do not get the error message. Adding GC.Collect did not help. However, there is a big difference between window A and Window B. Exiting from window A exits the program entirely. Exit window B returns to window A. I think this is a teachable moment for me. I think when returning to window A it is still trying to access window B but it can't because it has been disposed. I think I need to research what Dispose actually does and better understand how garbage collection can make applications more stable.
have you tried calling Hide before Dispose on you btnExit_Click event?
Yes and the error message remained. It looks like removing dispose it all I need. The screen disappears and stays on the calling window. If I need to open it again it appears.
You have other code elsewhere causing the issue. I just pasted your code into a dummy project I have and I have no errors.
Thanks everyone for your tips on this.