MsgBox关闭表单

时间:2013-10-24 20:59:57

标签: .net vb.net winforms .net-2.0

在我的程序(form1)中,我使用Form3.ShowDialog()调用另一个表单(form3)。此表单运行相对较长的过程,由进度条跟踪。在此表单中,有一个按钮可以取消此过程(生成pdf文档)并还原该过程。

取消按钮(form3)的代码如下:

Private Sub annulerBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles annulerBtn.Click
        If (MsgBox("Êtes-vous sûr de vouloir annuler? Cette reviendra toutes les modifications apportées au document", MsgBoxStyle.YesNo, "annuler l'exportation") = MsgBoxResult.Yes) Then

            _cancel = True

            doc.Close()       'close pdf'
            fs.Close()        'close stream'

            If (_done And _backup) Or (Not _done And _backup) Then

                'revert file from backup if backup exists'
                System.IO.File.Delete(_path)
                IO.File.Copy("C:\temp\temp.pdf", _path)
                IO.File.Delete("C:\temp\temp.pdf")

            Else

                'otherwise simply delete the new file'
                System.IO.File.Delete(_path)

            End If

            Me.Close()
        Else
            'continue with the form!!'
        End If
    End Sub

我希望此按钮结束流程并使用备份恢复更改。

我目前远离多线程并且在进程中使用Application.DoEvents()继续接受用户输入。

如果用户按下是按钮,则该功能按预期工作。但是,如果用户按否,则该过程将按预期继续,但表单将在之后关闭!

调试显示,在用户按下否之后,它永远不会调用Me.Close()Form3.Close()

对此问题的任何帮助将不胜感激,谢谢!

编辑:这是调用堆栈

    App58.exe!App58.Form3.Form3_FormClosing(Object sender = {App58.Form3}, System.Windows.Forms.FormClosingEventArgs e = {System.Windows.Forms.FormClosingEventArgs}) Line 432  Basic
    System.Windows.Forms.dll!System.Windows.Forms.Form.OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) + 0x77 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Form.CheckCloseDialog(bool closingOnly = false) + 0x8c bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FContinueMessageLoop(int reason, int pvLoopData, System.Windows.Forms.NativeMethods.MSG[] msgPeeked) + 0x160 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = 4, int pvLoopData = 0) + 0x1ae bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = 4, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.Application.ModalApplicationContext}) + 0x177 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form form) + 0x33 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) + 0x370 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x7 bytes 
    App58.exe!App58.Form1.RB_pdf_Click(Object sender = {Text = "Exporter PDF"}, System.EventArgs e = {System.Windows.Forms.MouseEventArgs}) Line 1994 + 0xa bytes   Basic

1 个答案:

答案 0 :(得分:3)

一个疯狂的猜测,你告诉我它是否正确。

按钮annulerBtn的属性DialogResult设置为与None不同的内容。
或者,form3的属性CancelButtonAcceptButton设置为annulerBtn

如果其中一个条件属实,那么无论您是否使用Close方法,当您点击该按钮时,您的表单都会自动关闭。如果要停止此事件链,则应在退出click事件之前将form3.DialogResult属性设置为DialogResult.None。 (或者删除表单与上面属性设置的按钮之间的关联)