为什么隐藏子窗体也会隐藏父窗体?

发布于 2024-09-15 00:30:03 字数 1341 浏览 10 评论 0原文

我有三种形式: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 技术交流群。

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

发布评论

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

评论(3

木有鱼丸 2024-09-22 00:30:03

我无法用您提供的代码重现您所看到的行为。任何表单上是否有其他设置(可能在设计器中设置)可能导致此问题?

此外,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 the Visible property to False. Read about Hide here.

If you truly want to be closing the window, you should be calling Me.Close().

旧人九事 2024-09-22 00:30:03

尝试设置子窗体的 mdiParent 属性:

    If IsNothing(_cases) Then
        _cases = New frmGrid        
        _cases.MdiParent = Me
        _cases.init(_main, 0, "", "")
        _cases.WindowState = FormWindowState.Maximized
    End If
    _cases.Visible = Me.mnuViewCaseFiles.Checked

Try setting the mdiParent property of the child form:

    If IsNothing(_cases) Then
        _cases = New frmGrid        
        _cases.MdiParent = Me
        _cases.init(_main, 0, "", "")
        _cases.WindowState = FormWindowState.Maximized
    End If
    _cases.Visible = Me.mnuViewCaseFiles.Checked
绝不服输 2024-09-22 00:30:03

我唯一能想到的是,您在表单 B 中有一个事件处理程序,它连接到表单 C 的按钮单击事件......尽管您将如何在没有意识到的情况下做到这一点,我无法想象。

当您在表单 B 的 IssueOrderButton_Click 事件中调用显示对话框时,尝试设置表单 C 的父级,方法是

f.ShowDialog(Me)

我同意您可能想要使用 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 doing

f.ShowDialog(Me)

I agree that you probably want to use Me.Close() rather than Me.Hide()

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