Can we know if a window has been closed by the use

2020-04-18 05:51发布

I have a window control that I show on my app. Sometimes, the window is closed by the user (clicking on the 'X' button) and sometimes automatically by code.

I'm listening to the Closed event (also tried with Closing) and would like to know in which case I am (user or code). Is this possible?

(I am aware of this question but it really doesn't have a satisfactory answer)

EDIT:

I'm looking for a simple solution, or some way of knowing where the event originated. I know I can implement this functionality myself as Sandeep Bansal suggested.

2条回答
家丑人穷心不美
2楼-- · 2020-04-18 06:17

Why not create a boolean value and then set it if the certain activity occurs?

bool closedByUser = false;

and then where the code is meant to close the form just add closedByUser = false; and closedByUser = true; if the action is user derived.

查看更多
Luminary・发光体
3楼-- · 2020-04-18 06:33

Option 1: Define a new type

 public class CodeClosingEventArgs : EventArgs
 { 
  // Fill any custom data you want
 }

And then call OnClosed(new CodeClosingEventArgs()); explicit in your code and check for the Event Type, if its a "normal" event arg, its the user by clicking the 'X'.

Option 2:

use a custom method which closes your form.

 public void MyClosingFormMethod()
 {
    base.OnClosed(null); 
 }
查看更多
登录 后发表回答