如果我关闭两种形式中的一种,两者都接近

时间:2014-01-02 17:15:30

标签: vb.net winforms dialog

我的申请表中有两种表格。如果我尝试关闭子表单(通过单击右上角交叉),则两者都关闭。

要显示子表单,我在点击按钮后使用Form.ShowDialog()。这是第一次发生,我无法解释这一点。

Private Sub btnShowChildForm_Click(ByVal sender As System.Object, ByVal e As Sysyem.EventArgs) Handles btnShowChildForm_Click

    '...

    Dim formChild As New frmChild
    formChild.ShowDialog()

End Sub

1 个答案:

答案 0 :(得分:1)

Form的父btnShowChildForm中删除此内容:

Me.CancelButton = Me.btnShowChildForm

这就是我重现类似行为的方式:

Public Class Form1
    Inherits Form

    Public Sub New()
        Me.testButton = New Button()
        Me.testButton.Text = "CLICK ME"
        Me.testButton.Location = New Point(3, 3)
        Me.Controls.Add(Me.testButton)
    End Sub

    Private Sub btnShowChildForm_Click(sender As System.Object, e As System.EventArgs) Handles testButton.Click
        Dim f As New Form2()
        f.ShowDialog()
    End Sub

    Private WithEvents testButton As Button

End Class

Public Class Form2
    Inherits Form

    Public Sub New()
        Me.btnShowChildForm = New Button()
        Me.btnShowChildForm.Text = "CLICK ME"
        Me.btnShowChildForm.Location = New Point(3, 3)
        Me.Controls.Add(Me.btnShowChildForm)
        Me.CancelButton = Me.btnShowChildForm
    End Sub

    Private Sub btnShowChildForm_Click(sender As System.Object, e As System.EventArgs) Handles btnShowChildForm.Click
        Dim formChild As New Form()
        formChild.ShowDialog()
    End Sub

    Private WithEvents btnShowChildForm As Button

End Class