Results 1 to 7 of 7

Thread: How to catch window closing?

  1. #1

    Thread Starter
    Addicted Member JackIlPazzo's Avatar
    Join Date
    Oct 2014
    Posts
    183

    How to catch window closing?

    I'm trying to intercept the closing of a window and excute a code inside this but I can't.
    Can someone explain me where is the best solution?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to catch window closing?

    For WPF, use the Closing event?

    Code:
      Private Sub MainWindow_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        'execute code here befoe we close
        'or possibly cancel the close and allow other parts of the code to do some clean up and then reissue the close
        If Not ReadyToClose Then   'assume we have a boolean that we'll set in code when closing allowed.
          e.Cancel = True 'don't close yet
          ReadyToClose = True  'Close the next time
        End If
    
      End Sub
    Last edited by passel; Jul 8th, 2015 at 02:14 PM.

  3. #3

    Thread Starter
    Addicted Member JackIlPazzo's Avatar
    Join Date
    Oct 2014
    Posts
    183

    Re: How to catch window closing?

    I'm using c# is the same if I convert the code?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to catch window closing?

    I would think so. Getting late, so I won't try to investigate.

  5. #5

    Thread Starter
    Addicted Member JackIlPazzo's Avatar
    Join Date
    Oct 2014
    Posts
    183

    Re: How to catch window closing?

    Quote Originally Posted by passel View Post
    I would think so. Getting late, so I won't try to investigate.
    Thanks eh

  6. #6
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: How to catch window closing?

    try it
    Code:
    public partial class DataWindow : Window
        {
            // Is data dirty
            bool isDataDirty = false;
    
            public DataWindow()
            {
                InitializeComponent();
            }
    
            void documentTextBox_TextChanged(object sender, EventArgs e)
            {
                this.isDataDirty = true;
            }
    
            void DataWindow_Closing(object sender, CancelEventArgs e)
            {
                MessageBox.Show("Closing called");
    
                // If data is dirty, notify user and ask for a response
                if (this.isDataDirty)
                {
                    string msg = "Data is dirty. Close without saving?";
                    MessageBoxResult result = 
                      MessageBox.Show(
                        msg, 
                        "Data App", 
                        MessageBoxButton.YesNo, 
                        MessageBoxImage.Warning);
                    if (result == MessageBoxResult.No)
                    {
                        // If user doesn't want to close, cancel closure
                        e.Cancel = true;
                    }
                }
            }
        }

  7. #7
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: How to catch window closing?

    try it
    Code:
    public partial class DataWindow : Window
        {
            // Is data dirty
            bool isDataDirty = false;
    
            public DataWindow()
            {
                InitializeComponent();
            }
    
            void documentTextBox_TextChanged(object sender, EventArgs e)
            {
                this.isDataDirty = true;
            }
    
            void DataWindow_Closing(object sender, CancelEventArgs e)
            {
                MessageBox.Show("Closing called");
    
                // If data is dirty, notify user and ask for a response
                if (this.isDataDirty)
                {
                    string msg = "Data is dirty. Close without saving?";
                    MessageBoxResult result = 
                      MessageBox.Show(
                        msg, 
                        "Data App", 
                        MessageBoxButton.YesNo, 
                        MessageBoxImage.Warning);
                    if (result == MessageBoxResult.No)
                    {
                        // If user doesn't want to close, cancel closure
                        e.Cancel = true;
                    }
                }
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width