Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi All,
I Have two winforms with buttons. I'm able to navigate between forms though buttons. Now i want to capture windows close(X) button to navigate to next form. But when I handle the windows close or closing events the projects stops. Please help me with this.
Thaks
Posted
Comments
[no name] 1-Jul-15 7:13am    
Please do not think that we are all psychic and can remote view your code. If you have some sort of problem with your code, it's really, really helpful to be able to actually see the code you are talking about.

When you close the main form - the one that is opened when you application starts, via the call to Application.Run in your program.cs file - the application closes and there isn't a lot you can do about that (other than some nasty playing round in the Main method to create new Applications).

So don't close the main form until the user is finished: Hide it instead and use ShowDialog to display the second form:
C#
MySecondForm msf = new MySecondForm();
Hide();
msf.ShowDialog();
Show();

That way, when your second form closes, your first displays again, and the user can still close the main form to exit the application.
 
Share this answer
 
If you want to implement navigation, ask about implementation of navigation, not about closing and showing forms.

Probably the best way to implement frame-by-frame kind of navigation and many different kinds of navigation is using only one form. Simply show different content on the form on each step. In simplest case, you can have several instanced of Panel docked using DockStyle.Fill (anyway, everything if Forms should be docked, to ensure stable and well maintainable layout), showing only one at a time. Navigation can hide current panel, showing another one. Then navigation controls themselves should of course be always visible.

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx[^].

To come back to several forms, the problems are: if you close a form, you cannot show it again; and if you close main form, it will exit the application. Therefore, if you use this for navigation (again, I don't recommend it), you should hide forms instead of closing. You don't need [x] non-client control box at all, but to prevent closing a form, you can replace the action with hiding in the following way: override the event FormClosing or override the virtual method Form.OnFormClosing and then cancel closing. It's done by assigning of the event arguments' property Cancel to true, but only if CloseReason equals to CloseReason.UserClosing, to allow closing by other reasons. Hide the form instead.

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28v=vs.110%29.aspx[^].

Knowing this technique is useful in many cases, but, again, using multiple forms for navigation is not what I recommend. Better use a single form with changed visibility of content. Or, alternatively/additionally, re-populate the controls in different ways for different frames on the fly.

—SA
 
Share this answer
 
Write the code to open next form on form closing event
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900