Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, October 18, 2010 8:16 AM
Hi,
I don't know why but the "Close" button on the Top Right of the form doesn't do anything.
Clicking on the close button does nothing as it should be closing the Form.
Alt+ F4 and exitMenu button are working fine to close the Form. :/
What could be wrong?
All replies (13)
Monday, October 18, 2010 9:57 AM ✅Answered
You are doing it the wrong way. The logic to decide whether or not to close the form should be in the Form.Closing event handler. As a side note, it's recommended to override OnFormClosing instead of handling the FormClosing event.
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (files.Count > 0)
{
if (files[CurrentFileId].Window == null)
files[CurrentFileId].Window = this;
DialogResult dlg = files[CurrentFileId].ParseFile();
switch (dlg)
{
case DialogResult.No:
saveToolStripMenuItem.PerformClick();
return;
case DialogResult.Cancel:
e.Cancel = true;
return;
}
}
}
Monday, October 18, 2010 8:27 AM
Many things, but without an idea what form it is, then we don't know.
If you have not any piece of code in your application it should close, with the exception if the mouse has a failure.
Otherwise, the reasons can be endless.
So what kind of form MDI, modal non modal.
etc. etc. etc.
Success
Cor
Monday, October 18, 2010 8:36 AM
Here's the program. Link
Just run the program and try clicking on the Close button at the top.
Monday, October 18, 2010 9:10 AM
Dear Optimus_Prime,
The problem is in the following events
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (!doClose)
{
<strong>e.Cancel = true;
</strong>
}
}
If we set e.Cancel = true, Form will not close. Just to ensure please set e.Cancel = false and run the application.
Thanks and Regards, Bharath S.
Monday, October 18, 2010 9:25 AM
"Are you sure you want to quite without saving?"
If the user click Yes, then the From should close.
If the user clicks No, the file should be saved.
If the user clicks Cancel, then the Form shouldn't be closed. e.Cancel = true
So, I can't set e.Cancel to be false. :/
Monday, October 18, 2010 9:31 AM
Copy this code and paste it into MainWindow_FormClosing event, and try
DialogResult dR;
dR = MessageBox.Show("Are you sure you want to quite without saving?","Test", MessageBoxButtons.YesNoCancel) ;
if (dR == DialogResult.Yes)
{
e.Cancel = false;
}
else if (dR == DialogResult.No)
{
//Save code
}
else
{
e.Cancel = true;
}
Thanks and Regards, Bharath S.
Monday, October 18, 2010 9:36 AM
That functionality is already there in the sample I provided.
I was saying that there was something wrong with it because of which it doesn't close the form.
You might want to go through sample program again from the link I provided in one of my posts
Monday, October 18, 2010 9:51 AM
Please once again review your code, I think its working as we written code.
When you click on Exit menu you changing the this.doClose = true. So in closing event your if condition become false (doclose=true) so obviously e.cancel = false in this case, so form will close.
When you click on close icon on form, doClose = false, so in closing event you assigning e.cancel=true. Thats why form not closing in this case.
Thanks and Regards, Bharath S.
Monday, October 18, 2010 10:02 AM
I tried setting e.Cancel = False;
And then clicking on Close button never asks me to save the file and just closes itself.
Since I set this.doClose= true on ExitMenu and Alt+F4, these work fine.
How should I get aroudn this?
Monday, October 18, 2010 10:22 AM
You need to move the code available in Exit menu to the common Function. Then you need to call this common function from both Exit menu and Form closing event.
Please let me know if you need detailed code to achieve this.
Thanks and Regards, Bharath S.
Monday, October 18, 2010 10:23 AM
You are doing it the wrong way. The logic to decide whether or not to close the form should be in the Form.Closing event handler. As a side note, it's recommended to override OnFormClosing instead of handling the FormClosing event.
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this .Close(); } protected override void OnFormClosing(FormClosingEventArgs e) { if (files.Count > 0) { if (files[CurrentFileId].Window == null ) files[CurrentFileId].Window = this ; DialogResult dlg = files[CurrentFileId].ParseFile(); switch (dlg) { case DialogResult.No: saveToolStripMenuItem.PerformClick(); return ; case DialogResult.Cancel: e.Cancel = true ; return ; } } }
I tried the above. If I create a new file, press Alt + F4 or Exitbutton from Menu, and press No (I have to save), then instead of saveToolStripMenuItem.PerformClick();
it closes the form.
On clicking the close button too, it doesn't prompt to save the file. x|
Monday, October 18, 2010 10:33 AM
You are doing it the wrong way. The logic to decide whether or not to close the form should be in the Form.Closing event handler. As a side note, it's recommended to override OnFormClosing instead of handling the FormClosing event.
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this .Close(); } protected override void OnFormClosing(FormClosingEventArgs e) { if (files.Count > 0) { if (files[CurrentFileId].Window == null ) files[CurrentFileId].Window = this ; DialogResult dlg = files[CurrentFileId].ParseFile(); switch (dlg) { case DialogResult.No: saveToolStripMenuItem.PerformClick(); return ; case DialogResult.Cancel: e.Cancel = true ; return ; } } }
Understood, done
Thanks
Monday, October 18, 2010 1:35 PM
On clicking the close button too, it doesn't prompt to save the file. x|
It's what happens if you never use the 'Create' button.
There is a flaw in both your code and my rewriting. If the user cancels the SaveFileDialog, it should return to the application, not quit without saving. Move the contents of the saveToolStripMenuItem_Click method to another method returning a bool. Make it return, for example, true if the file has been saved. Use the result of that method to set Cancel:
DialogResult dlg = files[CurrentFileId].ParseFile();
switch (dlg)
{
case DialogResult.No:
e.Cancel = !SaveFile();
return;
case DialogResult.Cancel:
e.Cancel = true;
return;
}