How to know user has clicked “X” or the “Close” button?

前端 未结 12 1019
野性不改
野性不改 2020-11-27 12:19

In MSDN I found CloseReason.UserClosing to know that the user had decided to close the form but I guess it is the same for both clicking the X button or clickin

12条回答
  •  失恋的感觉
    2020-11-27 12:54

    if (this.DialogResult == DialogResult.Cancel)
            {
    
            }
            else
            {
                switch (e.CloseReason)
                {
                    case CloseReason.UserClosing:
                        e.Cancel = true;
                        break;
                }
            }
    

    if condition will execute when user clicks 'X' or close button on form. The else can be used when user clicks Alt+f4 for any other purpose

提交回复
热议问题