【问题标题】:VB.NET ShowDialog Form not EndingVB.NET ShowDialog 表单未结束
【发布时间】:2010-12-07 02:36:15
【问题描述】:

我从 VB6 转换了这个应用程序。我有 2 个表格。 Form1 通过菜单项实例化 Form2。 单击关闭 (X) 时,我无法让 Form2 结束。如果 Form2 是“空闲”,它会很好地关闭;但是如果我在一个循环中处理任何所有事件都会触发,但它会继续在 Form2 中处理。我尝试过处理 Dispose、Close、Application.Exit、Application.ExitThread。我的最后一次尝试是创建我自己的事件来回火到 Form1 并处理 Form2——它击中了它,但 Form2 仍在运行。什么是交易?顺便说一句,如果我只使用 Show vs ShowDialog -- Form2 只是闪烁并消失。

Form1 does this
Dim f2 as Import
:
        Hide()
        f2 = New Import
        AddHandler f2.die, AddressOf killf2
        f2.ShowDialog(Me)
        Show()

Private Sub killf2()
        f2.Dispose()
        f2 = Nothing
End Sub

Form2

Public Event die()
Private Shadows Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dispose()
        Close()
        e.Cancel = False
        RaiseEvent die()
End Sub

【问题讨论】:

    标签: vb.net forms showdialog


    【解决方案1】:

    我认为你已经完成了你的事件。您希望包含 form2 实例的 form1 监听 form2 的 form_closure 事件。然后你可以设置 f2 = nothing。

    Form1 应该完全包含 form2。

    这是一个例子:

    Public Class MDIMain
        Private WithEvents _child As frmViewChild
    
        Friend Sub viewChildShow()
            _child = New frmViewChild
            _child.MdiParent = Me
            _child.WindowState = FormWindowState.Maximized
            _child.Show()
        End Sub
    
        Private Sub _child_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles _child.FormClosing
            _child = Nothing
        End Sub
    

    不要在form2中添加任何东西,试试

    Dim f2 as Import
            Hide()
            f2 = New Import
            f2.ShowDialog(Me)
            Show()
    
    Private Sub f2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles f2.FormClosing
            set f2 = nothing
    End Sub
    

    回复:您的评论 它返回到 form2 并继续处理 click 事件处理程序中的下一条语句

    这是一个特性,它会导致这种行为。您需要确保 me.close 或 close me 是 form2 中的最后一条语句,没有其他要执行的语句。

    【讨论】:

    • 我刚刚在复制和粘贴时调用了事件 Form1_FormClosing。它实际上处理 MyBase.FormClosing - 对于 form2。如果这不是你的意思——那么我如何在 form1 中监听 form 2 的 form_closing 事件,而不是像我所做的那样仅仅引发一个事件并设置 f2=nothing 。谢谢!
    • Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) 处理 f2.FormClosing f2.Dispose() f2.Close() f2 = Nothing End Sub
    • 我只是不明白为什么表单没有结束。我做了你所拥有的,在它击中 form1 中的 f2=nothing 后,它返回到 form2 并继续处理 click 事件处理程序中的下一条语句
    • 我尝试过并且似乎可行的一件事是定义一个布尔退出,然后在 Form1 事件处理程序中将退出设置为 true。在循环中 if quit = true 退出子。就这样处理吗????
    • 说话太快了——一旦我尝试单击该 Form2 并再次启动它,我就会发现该进程无法访问文件,因为它正在被另一个进程使用
    【解决方案2】:

    你说的这个循环是什么?用户界面(Windows)与任何正在运行的代码是分开的。在您的类派生表单 Form 中,允许在创建表单之前和销毁表单之后运行代码。如果代码尝试访问用户界面对象,则可能会发生异常,但如果没有用户界面,则不会阻止您的代码运行。

    如果你想让你的“for”循环退出,那么你必须以某种方式向它发送一个信号,例如通过创建一个布尔“退出”成员变量。当你的表单关闭时设置“quit=True”,然后让你的“for”循环检查它是否为真。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2014-10-22
      • 2017-04-29
      • 2016-03-21
      相关资源
      最近更新 更多