为什么隐藏子窗体也会隐藏父窗体?
我有三种形式:A、B 和 C。
单击按钮后,A 显示 B。类似地,B 显示 C。两者都使用 ShowDialog() 方法。
但是,当我在表单 C 上使用 Hide() 方法(在按钮单击事件内)将其关闭时,表单 B 也会关闭。
为什么会这样呢?据我所知,它不应该这样做。我当然没有编写任何代码来告诉它这样做。
这是代码:
' from Form "A" (MainForm)
Private Sub OrdersDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDataGridView.CellDoubleClick
ShowViewOrderForm(sender)
End Sub
Private Sub ShowViewOrderForm(ByVal CurrentDGV As DataGridView)
If Not CurrentDGV Is Nothing Then
Dim f As New ViewOrderForm
f.SetOrderNo(CurrentDGV.CurrentRow().Cells().Item(0).Value)
f.SetDeliveryServiceType(CurrentDGV.CurrentRow().Cells().Item(5).Value)
f.ShowDialog()
End If
End Sub
' from Form "B" (ViewOrderForm)
Private Sub IssueOrderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IssueOrderButton.Click
Dim f As New IssueForm
f.SetOrderNo(ThisOrderNo)
f.ShowDialog()
End Sub
' from Form "C" (IssueForm)
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
Me.Hide()
End Sub
更新:我是个白痴。按钮上的 DialogResult
设置为 Cancel
,因为我从现有的“关闭”按钮中复制并粘贴了它,但没有意识到该属性已设置。无论如何,谢谢大家的建议!
I have three forms: A, B and C.
Upon a button being clicked, A displays B. Similarly, B displays C. Both using the ShowDialog() method.
However when I use the Hide() method (inside a Button Click event) on form C to close it, form B also closes.
Why would this be? As far as I can see, it shouldn't be doing this. I certainly didn't write any code to tell it to do that.
Here is the code:
' from Form "A" (MainForm)
Private Sub OrdersDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDataGridView.CellDoubleClick
ShowViewOrderForm(sender)
End Sub
Private Sub ShowViewOrderForm(ByVal CurrentDGV As DataGridView)
If Not CurrentDGV Is Nothing Then
Dim f As New ViewOrderForm
f.SetOrderNo(CurrentDGV.CurrentRow().Cells().Item(0).Value)
f.SetDeliveryServiceType(CurrentDGV.CurrentRow().Cells().Item(5).Value)
f.ShowDialog()
End If
End Sub
' from Form "B" (ViewOrderForm)
Private Sub IssueOrderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IssueOrderButton.Click
Dim f As New IssueForm
f.SetOrderNo(ThisOrderNo)
f.ShowDialog()
End Sub
' from Form "C" (IssueForm)
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
Me.Hide()
End Sub
UPDATE: I am an idiot. DialogResult
was set to Cancel
on the button as I'd copy+pasted it from the existing Close button and not realised that property was set. Thanks for your suggestions anyway everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法用您提供的代码重现您所看到的行为。任何表单上是否有其他设置(可能在设计器中设置)可能导致此问题?
此外,
Hide()
函数实际上并不关闭窗口。这相当于将Visible
属性更改为 False。 此处了解有关隐藏的信息。如果您确实想关闭窗口,则应该调用
Me.Close()
。I have not been able to reproduce the behavior you are seeing with the code that you've supplied. Are there other settings on any of the forms (perhaps set in the designer) that could be causing this?
Also, the
Hide()
function does not actually close the window. It is the equivalent of changing theVisible
property to False. Read about Hide here.If you truly want to be closing the window, you should be calling
Me.Close()
.尝试设置子窗体的 mdiParent 属性:
Try setting the mdiParent property of the child form:
我唯一能想到的是,您在表单 B 中有一个事件处理程序,它连接到表单 C 的按钮单击事件......尽管您将如何在没有意识到的情况下做到这一点,我无法想象。
当您在表单 B 的
IssueOrderButton_Click
事件中调用显示对话框时,尝试设置表单 C 的父级,方法是我同意您可能想要使用
Me.Close()
而不是Me .隐藏()
The only thing I can think of is that you have an event handler in Form B that is hooked up to Form C's button click event... though how you would do that without realizing I can't imagine.
Try setting Form C's parent when you call show dialog in Form B's
IssueOrderButton_Click
event by doingI agree that you probably want to use
Me.Close()
rather thanMe.Hide()