的WinForms表格不会关闭在C#按X或关闭() [英] WinForms Form won't close on pressing X or Close() in C#

查看:274
本文介绍了的WinForms表格不会关闭在C#按X或关闭()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与WinForm的一点奇怪的问题,这似乎拒绝关闭了一些奇怪的原因。我有很简单的GUI有时不为我反应过来按X,或当我使用事件上的按钮甚至达到关闭()和无助..

 私人无效buttonZapisz_Click(对象发件人,EventArgs五){
串plik = textBoxDokumentDoZaladowania.Text;
如果(File.Exists(plik)){
线延长= Path.GetExtension(plik);
串nazwaPliku = Path.GetFileName(plik);

SqlMethods.databaseFilePut(plik,comboBoxTypDokumentu.Text,textBoxKomentarz.Text,sKlienciID,sPortfelID,推广,nazwaPliku);
关闭();
}
}

有没有分配给 FormClosed 或的FormClosing 。所以,我怎么能找出什么是错的。有时,GUI被加载后,X将工作,但之后我按按钮来保存一些东西到数据库达到关闭()在按钮的事件,它仍然是可见的,所以没有任何。不能用X,也不ALT + F4。我可以去周围GUI和选择组合框其它数值没有问题。



我把GUI是这样的:

 私人无效contextMenuDokumentyDodaj_Click(对象发件人,EventArgs五){
VAR LV =(ListView控件)contextMenuDokumenty.SourceControl;
串varPortfelID = Locale.ustalDaneListViewKolumny(listViewNumeryUmow,0);
串varKlienciID = Locale.ustalDaneListViewKolumny(listViewKlienci,0);

如果(LV == listViewDokumentyPerKlient){
如果(varKlienciID =!){
变种dokumenty =新DocumentsGui(varKlienciID);
dokumenty.Show();
dokumenty.FormClosed + = varDocumentsGuiKlienci_FormClosed;
}
}否则如果(LV == listViewDokumentyPerPortfel){
如果(varPortfelID =!&放大器;&安培;!varKlienciID =){
变种dokumenty =新DocumentsGui (varKlienciID,varPortfelID);
dokumenty.Show();
dokumenty.FormClosed + = varDocumentsGuiPortfele_FormClosed;
}
}
}



虽然我不能关闭GUI我可以在主界面的工作没有问题了。我可以打开相同的GUI并打开新的GUI后,我可以很快将其关闭。 GUI是很简单的用几个组合框文本框和一个 EditButton 从DevExpress的



编辑: varDocumentsGuiPortfele_FormClosed代码可以让我更新的GUI(重装ListView的具体情况取决于用户是在目前如此)。

 私人无效varDocumentsGuiPortfele_FormClosed(对象发件人,FormClosedEventArgs E){
的TabControl varTabControl = tabControlKlientPortfele;

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

}
}


解决方案

这段代码粘贴到你的表单类:

 保护覆盖无效OnFormClosing(FormClosingEventArgs E){
é。取消= FALSE;
base.OnFormClosing(E);
}

当这样的作品,你要找出为什么你已经验证事件处理程序不想被关闭的形式。



您想验证下一件事情就是调试+异常,剔为CLR异常的抛出该异常框。这可以确保你不下咽,防止形式从闭合异常。或者更糟的是,操作系统吞咽异常,一个讨厌Windows 7的问题。


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();

      }
    }

解决方案

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.

这篇关于的WinForms表格不会关闭在C#按X或关闭()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆