Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determine Form X Click
Message
De
07/11/2009 04:32:45
 
 
À
06/11/2009 23:52:44
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01433647
Message ID:
01433656
Vues:
31
>>In C#, how do I determine if the user clicked the 'X' to close a form?
>
>I found this somewhere online (stackoverflow or msdn or someplace else - I don't remember) sometime last month and tested it for fun:
>Create a simple form named Form1 with a button named buttonExit to test this code.
>
>//Example Form1.cs
>using System;
>using System.Collections.Generic;
>using System.ComponentModel;
>using System.Data;
>using System.Drawing;
>using System.Linq;
>using System.Text;
>using System.Windows.Forms;
>
>namespace WindowsFormsApplication1
>{
>    public partial class Form1 : Form
>    {
>
>    public bool UserClosing { get; set; }
>
>    public Form1()
>    {       
>         InitializeComponent();        
>         UserClosing = false;        
>         this.buttonExit.Click += new EventHandler(buttonExit_Click);        
>         this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);   
>     }   
>
>     void buttonExit_Click(object sender, EventArgs e)    
>    {        
>        UserClosing = true;        
>        this.Close();    
>    }    
>
>    void Form1_FormClosing(object sender, FormClosingEventArgs e)   
>     {        
>          switch (e.CloseReason)       
>          {            
>               case CloseReason.ApplicationExitCall:
>                    break;
>               case CloseReason.FormOwnerClosing:
>                    break; 
>               case CloseReason.MdiFormClosing:
>                    break;
>                case CloseReason.None:
>                    break;
>                case CloseReason.TaskManagerClosing:
>                    break;
>                case CloseReason.UserClosing: 
>                   if (UserClosing) 
>                   {
>                       MessageBox.Show("The user clicked the Close Button");
>                    } 
>                   else 
>                   {
>                       MessageBox.Show("The user clicked the X");
>                   } 
>                   break;
>                case CloseReason.WindowsShutDown:
>                    break; 
>                default:
>                    break;
>            }
>        }
>    }
>}
>
>
>I did a google search and after a few tries, found it:
>
>*Courtesy Oliver
>http://stackoverflow.com/questions/1623756/c-detect-reason-for-form-closing

I always liked the CloseReason.None concept - surely would have been more sensible to use CloseReason.Unknown ?
Regards,
Viv
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform