WinForms form will not close when X or Close () is pressed in C #

I have a slightly strange problem with WinForm that seems to refuse to close for some strange reason. I have a very simple gui that sometimes doesn’t respond when I press X, or when I use events on buttons, it even reaches Close() and does nothing ..

  private void buttonZapisz_Click(object sender, EventArgs e) { string plik = textBoxDokumentDoZaladowania.Text; if (File.Exists(plik)) { string extension = Path.GetExtension(plik); string nazwaPliku = Path.GetFileName(plik); SqlMethods.databaseFilePut(plik, comboBoxTypDokumentu.Text, textBoxKomentarz.Text, sKlienciID, sPortfelID, extension, nazwaPliku); Close(); } } 

There are no events assigned by FormClosed or FormClosing . So how can I find out what happened. Sometimes X will work after loading the GUI, but after I press Button to save some data in the database, it will reach Close () in this event, and it still displays and does nothing. You cannot use X, or ALT + F4. I can get around the GUI and select other values ​​for the ComboBox without any problems.

I invoke the GUI as follows:

  private void contextMenuDokumentyDodaj_Click(object sender, EventArgs e) { var lv = (ListView) contextMenuDokumenty.SourceControl; string varPortfelID = Locale.ustalDaneListViewKolumny(listViewNumeryUmow, 0); string varKlienciID = Locale.ustalDaneListViewKolumny(listViewKlienci, 0); if (lv == listViewDokumentyPerKlient) { if (varKlienciID != "") { var dokumenty = new DocumentsGui(varKlienciID); dokumenty.Show(); dokumenty.FormClosed += varDocumentsGuiKlienci_FormClosed; } } else if (lv == listViewDokumentyPerPortfel) { if (varPortfelID != "" && varKlienciID != "") { var dokumenty = new DocumentsGui(varKlienciID, varPortfelID); dokumenty.Show(); dokumenty.FormClosed += varDocumentsGuiPortfele_FormClosed; } } } 

While I cannot close the GUI, I can work with the main gui too. I can open the same GUI and after opening a new GUI I can quickly close it. The GUI is very simple with several ComboBoxes , TextBoxes and one EditButton from Devexpress.

Edit: varDocumentsGuiPortfele_FormClosed code allows me to update the GUI (reload the ListView depending on where the user is now).

  private void varDocumentsGuiPortfele_FormClosed(object sender, FormClosedEventArgs e) { TabControl varTabControl = tabControlKlientPortfele; if (varTabControl.TabPages.IndexOf(tabPageDokumentyPerKlient) == varTabControl.SelectedIndex) { loadTabControlKlientPortfeleBezZmianyUmowy(); } } 
+4
source share
2 answers

Paste this code into your form classes:

  protected override void OnFormClosing(FormClosingEventArgs e) { e.Cancel = false; base.OnFormClosing(e); } 

When this works, you want to find out why you have Validating event handlers that do not want to close the form.

The next thing you want to check is Debug + Exceptions, check the "Abandoned box for CLR exceptions" box. This ensures that you do not swallow an exception that prevents the form from closing. Or, even worse, the operating system swallows an exception , an unpleasant problem with Windows 7.

+14
source

If you get an exception in the close method, then the database closure method is never called.

Try {} catch {} around everything

+1
source

Source: https://habr.com/ru/post/1381013/


All Articles