I've made a form closing event when X is pressed, but I also want the 'Exit' button to call this same method yet it draws me error every time I change stuff.
--- This code below is the form closing event ---
// if user pressed 'Exit' button or red cross
private void TempConverterForm1_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult exitdialog = MessageBox.Show("Are you sure you want to quit?", "Quit?", MessageBoxButtons.YesNoCancel);
if (exitdialog == DialogResult.Yes) {
e.Cancel = false;
}
else {
e.Cancel = true;
}
}
--- This code below is the code I'm trying to solve ---
// if the 'exit' button is pressed
private void btn_Exit_Click(object sender, EventArgs e) {
TempConverterForm1_FormClosing(sender, (FormClosingEventArgs) e);
}
I've tried without FormClosingEventArgs
first but on itself it says that EventArgs
can't be converted to closing event. I put FormClosingEventArgs
but now it tries to convert from MouseEventArgs
to FormClosingEventArgs
even though I relate to button click and not mouse click.
I tried to do research but the problem repeats and builds up with different error messages and I got lost and decided I need help with this.
Just do this.Close() in btn_Exit_Click. This will fire Form_Closing correctly with the right arguments, and your cancel will still work.
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}