Noel
Noel

Reputation: 384

How to correctly close a c# form

I am currently working with a c# desktop application that is connected to a web service. In one of the forms, I want to close form when the back button is clicked. I have done it like this:

private void bBack_Click(object sender, EventArgs e)
{
    //this.Hide();
    this.Close();
    frmMain fMain = new frmMain();
    fMain.MdiParent = this.MdiParent;
    fMain.Show();
}

When I use this.Hide(), it seems like the form I want to close still runs in the background, thus still contacting the web service.

When I use this.Close(), the form seems to close and no contact to the web service is made, but my desktop application suddenly goes full screen and frmMain is opened in a new window.

I have read the documentation displayed here, but I could not find anything useful.

Upvotes: 0

Views: 127

Answers (1)

BugFinder
BugFinder

Reputation: 17868

Hide does just that, it hides it, its still there.

So yes, you would need to close it, however, if you close it before you make the next, theres a chance that the code exits there and isnt completing any follow on code.

Rearranging your code to

frmMain fMain = new frmMain();
fMain.MdiParent = this.MdiParent;
fMain.Show();
this.Close();

Should work

Upvotes: 2

Related Questions