-
Jul 8th, 2015, 02:05 PM
#1
Thread Starter
Addicted Member
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?
-
Jul 8th, 2015, 02:10 PM
#2
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.
-
Jul 9th, 2015, 02:04 AM
#3
Thread Starter
Addicted Member
Re: How to catch window closing?
I'm using c# is the same if I convert the code?
-
Jul 9th, 2015, 02:48 AM
#4
Re: How to catch window closing?
I would think so. Getting late, so I won't try to investigate.
-
Jul 9th, 2015, 04:18 AM
#5
Thread Starter
Addicted Member
Re: How to catch window closing?
Originally Posted by passel
I would think so. Getting late, so I won't try to investigate.
Thanks eh
-
Mar 26th, 2020, 03:09 AM
#6
Lively Member
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;
}
}
}
}
-
Mar 26th, 2020, 03:11 AM
#7
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|