【问题标题】:MsgBox closes formMsgBox 关闭表单
【发布时间】:2013-10-24 20:59:56
【问题描述】:

在我的程序 (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() 继续接受用户输入。

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

调试显示它在用户按下 no 后它永远不会调用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

【问题讨论】:

  • 不清楚你想要什么,我觉得表达问题没那么难,这是什么the form as it is supposed to?而form 指的是什么? ...请尝试重新表述您的问题。
  • 您注释掉了最重要的代码。一个基本的调试策略是添加 FormClosing 事件处理程序并在其上设置断点。查看调用堆栈以了解它是如何到达那里的。
  • 所以你希望它调用Me.Close(),即使用户按下否?
  • @Douglas Barbin 不,这是我极力避免的。当用户选择“否”选项时,表单关闭
  • 作为一个通用警告,我会远离遗留的 MsgBox()。我已经看到许多奇怪的“错误”在使用正确的 MessageBox.Show() 时消失了。

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


【解决方案1】:

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

按钮annulerBtn 的属性DialogResult 设置为不同于None
或者form3的属性CancelButtonAcceptButton设置为annulerBtn

如果其中一个条件为真,那么无论您是否调用Close 方法,您的表单都会在您单击该按钮时自动关闭。如果要停止此事件链,应在退出单击事件之前将 form3.DialogResult 属性设置为 DialogResult.None。 (或者通过上面的属性去掉表单和按钮设置的关联)

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多