Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to write a function that runs on closing a wpf application. How can I do this for the Windows close button (which is there in the top right corner of a window)?
Posted
Updated 7-Mar-11 23:27pm
v2

You will get the event handler automatically when you will select the Closing event from the help list in the "window" tag at XAML page. It will automatically generate an event at the code behind when you will select "new EventHandler" by pressing ctrl+spacebar inside the 'closing=""' commas.

XAML file example:

VB
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Closing="Window_Closing" Closed="Window_Closed" >


Code behind example:

C#
private void Window_Closed(object sender, EventArgs e)
{
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e
{
}


Hope this helps :rose:

Anurag
 
Share this answer
 
v3
Comments
Tarun.K.S 8-Mar-11 6:27am    
Exactly! 5d!
@nuraGGupta@ 8-Mar-11 6:29am    
Thanks Tarun :-)
…probably I did not read the question properly in first place, down-voted for a reason. Here is the answer:

C#
public partial class MyWindow : Window {
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
        base.OnClosing(e);
        // the process of closing can be canceled;
        // uncomment two next lines for a typical use of it:
        //e.Cancel = true;
        //Hide();
    }


    protected override void OnClosed(EventArgs e) {
        base.OnClosed(e);
        //...
    }


Alternatively, this is the event-based approach (which is not really needed because Window is sub-classed anyway:

C#
MyWindow.Closed += (sender, eventArgs) => {
    //...
};
MyWindow.Closing += (sender, eventArgs) => {
    //..
    // the process of closing can be canceled;
    // uncomment two next lines for a typical use of it:
    //e.Cancel = true;
    //Hide();
};


Both ways are much better than the code auto-generated by the Designer. It's funny that the code not based on anonymous methods is still used. It is less supportable but probably easier of generation (or code generation uses legacy code, which is explainable). Anonymous-based way is shorter and helps supportability, because writing a method with exact signature and writing type names is not required. Additionally, in lambda form the types of arguments are inferred from event type.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 8-Mar-11 12:00pm    
Good option on how to implement the required functionality, my 5
Sergey Alexandrovich Kryukov 8-Mar-11 13:38pm    
Thank you, Espen,
I keep insisting on better syntax and usage, but people keep using obsolete and inferior Microsoft style.
Probably Microsoft authority is pressing, nothing else (not just Microsoft: "bad Microsoft" is pressing more than "good Microsoft"). This is a problem in real life. I know a number of developers completely unable of rational considerations, instead pressed by "because Microsoft does this" or, worse "because everyone does this".
--SA
Thnaks so much for your time But I need to write some code for close window functionality, i know how to close the application.

For OP: You are supposed to provide information by either "improving your question" or by "adding a comment" to your question, unless you are providing a solution to your problem. :)

You comment has been moved to the comments section of intended recipient. :cool:
 
Share this answer
 
v3

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