VB.NET ShowDialog 表单未结束

发布于 2024-09-30 19:58:14 字数 834 浏览 2 评论 0原文

我从 VB6 转换了这个应用程序。我有两种形式。 Form1 通过菜单项实例化 Form2。 单击关闭 (X) 时,我无法结束 Form2。如果 Form2 处于“空闲”状态,则可以正常关闭;但如果我在循环中处理任何事件,所有事件都会触发,但它会继续在 Form2 中处理。我尝试过使用 Dispose、Close、Application.Exit、Application.ExitThread。我的最后一次尝试是创建自己的事件来触发 Form1 并处理 Form2——它击中了它,但 Form2 仍在运行。这是什么交易?顺便说一句,如果我只使用 Show 与 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

I converted this app from VB6. I have 2 forms. Form1 instantiates Form2 via a Menu Item.
I am having trouble getting Form2 to end when clicking close (X). If Form2 is 'idle' it closes fine; but if I am in a loop processing anything all the events fire, but it continues processing in Form2. I've tried messing with Dispose, Close, Application.Exit, Application.ExitThread. My last attempt was creating my own event to fire back to Form1 and dispose Form2 -- and it hits it but Form2 is still running. What is the deal? BTW if I use just Show vs ShowDialog -- Form2 just blinks and disappears.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-10-07 19:58:14

我想你的事件已经交叉了。您希望包含 form2 实例的 form1 侦听 form2 的 form_ opening 事件。然后你可以设置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 并继续处理单击事件处理程序中的下一个语句

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

I think you've got your events crossed. You want form1, containing an instance of form2, to listen for form2's form_closing event. Then you can set f2 = nothing.

Form1 should fully enclose form2.

here's an example:

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

don't add anything to form2, try

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

re: your comment
it goes back to form2 and continues processing the next statement in the the click event handler

that's a feature and it will cause this behavior. you need to make sure me.close or close me is the last statement in form2, that there's nothing else to execute.

拒绝两难 2024-10-07 19:58:14

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

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

What is this loop you speak of? The user interface (windows) is separate from any code that is running. In your class derived form Form, Code is allowed to run, both before the Form is created, and after the Form is destroyed. If the code tries to access user-interface objects then an exception might occur, but otherwise there is nothing stopping your code from running when there is no user interface.

If you want your "for" loop to exit then you must send it a signal somehow, e.g. by creating a boolean "quit" member variable. Set "quit=True" when your form closes, then have your "for" loop check whether it is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文