Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

Can I ask if someone have an idea on how can I write a code in the close button in the form.

Example, if the user click the close button (the red box with X) then another form that i want be show.

Thanks!
Posted

I assume you are talking about WinForms here. If you are, each window has a Closing and Closed event associated with it. What I would do is override the OnClosed implementation and show my window from there.
C#
protected override void OnClosed(EventArgs args)
{
  new NewWindow().Show();
  base.OnClosed(args);
}
Importantly from your point of view, this method is not called when the application exits, so you won't get rogue windows opening up unexpectedly.
 
Share this answer
 
Attach whatever you want to the form's OnClosing[^] event. That way you don't have to differentiate what causes the form to close ("X", Alt + F4, right-click on Taskbar, ...).
C#
override void OnClosing()
{
    SomeOtherForm otherForm = new SomeOtherForm(whatever);
    otherForm.Show();
}
 
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