0

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.

4
  • 3
    thats because hide just hides it.. it is still running in the background. Your code here shows nothing of a webservice, but try making the main new form before closing this one. Commented Nov 23, 2017 at 9:44
  • the more I read this question the more unclear it becomes. Please clarify what your issue is. It seems the first 2 thirds of this question are irrlevant ot what you actually want and the question you actually want asking but my desktop application suddenly goes full screen and frmMain() is opened in a new window. contains no code. But even that is very, very unclear. What is the question? Commented Nov 23, 2017 at 9:49
  • @BugFinder i didn't include the ws implementation because it worked fine. Anyways I followed your advice and it looks like it works. I just thought in order to show the frmMain() you needed to close the existing form first. Commented Nov 23, 2017 at 9:50
  • @noel great - ive added it as an answer so you can give it the big hurrah Commented Nov 23, 2017 at 9:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Why assign MdiParent when you can assign Parent directly?
I just rearranged his code .. yes you can set parent.. but this is the MDIparent, I havent used mdi in a while so left his code as was..
MdiParent is the correct property to set, setting fMain.Parent on a form throws an exception. Perhaps @CamiloTerevinto meant to ask "why copy from this.MdiParent"? But as we don't have the OP's code, ...
@C.Evenhuis No, it doesn't throw any exception, who told you that? I assign Parent in many projects.
@CamiloTerevinto "Top-level control cannot be added to a control." Are you sure you're talking about assigning a value to a Form.Parent?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.