Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms Form won't close on pressing X or Close() in C#

I'm having a bit weird problem with WinForm which seems to refuse to close for some weird reason. I've got very simple gui which sometimes doesn't react for me pressing 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 to FormClosed or FormClosing. So how can I find out what's wrong. Sometimes X will work after the GUI is loaded but after i press Button to save some stuff to database it reaches Close() in that button event and it still is visible and does nothing. Can't use X, nor ALT+F4. I can go around GUI and choose other values for ComboBox without problem.

I call GUI like this:

    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 can't close GUI i can work on the main gui without problem too. I can open up same GUI and after opening new GUI i can quickly close it. GUI is very simple with few ComboBoxes,TextBoxes and one EditButton from Devexpress.

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

    private void varDocumentsGuiPortfele_FormClosed(object sender, FormClosedEventArgs e) {
        TabControl varTabControl = tabControlKlientPortfele;

      if (varTabControl.TabPages.IndexOf(tabPageDokumentyPerKlient) == varTabControl.SelectedIndex) {
          loadTabControlKlientPortfeleBezZmianyUmowy();

      }
    }
like image 278
MadBoy Avatar asked Dec 04 '22 06:12

MadBoy


2 Answers

Paste this code into your form classes:

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

When that works, you want to find out why you have Validating event handlers that don't want the form to be closed.

Next thing you want to verify is Debug + Exceptions, tick the Thrown box for CLR Exceptions. This makes sure you don't swallow an exception that prevents a form from closing. Or worse, the operating system swallowing the exception, a nasty Windows 7 problem.

like image 100
Hans Passant Avatar answered Dec 28 '22 05:12

Hans Passant


If you are getting an Exception in your close method, then the Base closing method is never called.

Put a try{}catch{} around everything

like image 43
penguin Avatar answered Dec 28 '22 04:12

penguin