PDA

View Full Version : [C#] Evento Chiusura Form


yagamiraito
10-01-2008, 14:58
Devo intercettare l'evento di chiusura form quando si clicca sulla X.
(per una DeviceApllication con compact framework)

class Form1 : Form { (...) this.Closing += new CancelEventHandler(Form1_Closing); (...) }


public void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Do you want to save changes to your text?", "My Application", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
e.Cancel = true;
}
}


Viene sempre chiuso tutto senza venir visualizzato il messaggio. Cosa sbaglio?

ReaToMe
10-01-2008, 19:22
Versione del framework? Nel caso fosse la 2 usa l'evento FormClosing.
In che metodo della classe aggiungi l'handler?

yagamiraito
11-01-2008, 10:12
Compact Framework V2 nel quale la classe Form non ha l'evento FormClosing.

L'handler lo aggiungo in Form1.Designer.cs nel metodo InitializeComponent().

ReaToMe
11-01-2008, 10:27
E' vero, formclosing non esiste nel compact.
Sei sicuro che il metodo InitializeComponent() venga invocato nel costruttore?
Certo che è strano...

ReaToMe
11-01-2008, 10:36
Credo che questo dovrebbe chiarire il problema:

http://www.msdner.com/dev-archive/55/11-33-558206.shtm

yagamiraito
11-01-2008, 10:52
Credo che questo dovrebbe chiarire il problema:

http://www.msdner.com/dev-archive/55/11-33-558206.shtm


Perfetto! Grazie Mille! :)


Se servisse ad altri

that is because clicking the x in the upper right corner doesn't close your form. It "smart minimizes" it. Your app is still running, it is just stuck at the bottom of the Z-order. If you want to close your form in that manner, you can either show it via ShowDialog() or you can set the MinimizeBox property to False. In both cases, the "x" will turn into an "ok" and clicking it will close your form.


Mi è bastato mettere
MinimizeBox = false;
e tutto ha funzionato. :)

ReaToMe
11-01-2008, 12:41
Ne sono contento!!!