VB.NET ShowDialog 表单未结束
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你的事件已经交叉了。您希望包含 form2 实例的 form1 侦听 form2 的 form_ opening 事件。然后你可以设置f2=nothing。
Form1 应完全包围 form2。
这是一个例子:
不要向 form2 添加任何内容,尝试
回复:您的评论
它返回到 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:
don't add anything to form2, try
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.
你说的这个循环是什么?用户界面(窗口)与任何正在运行的代码是分开的。在派生窗体 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.