【问题标题】:Send value to parent then close child form, on event在事件中向父级发送值然后关闭子窗体
【发布时间】:2015-04-28 23:12:20
【问题描述】:

我有一个父表单,其中包含一个从数据库填充的组合框,用户可以从中进行选择。组合框中的最后一个值是“添加新值”,如果用户选择此项,则会打开一个子表单供用户向数据库添加新值。我有一个按钮按下事件来将此值添加到数据库中,将新的return value 发送给父级并关闭表单。然后父级应该从它的组合框中选择新值并等待用户执行另一个操作。

但是,将return value 发送给父级并关闭表单的代码无法正常工作。我hide子,然后调用它与父函数访问return value。此时子窗体shows 和代码在运行另一个hideclose 之前停止。

我该如何解决这个问题(下面的代码)?

父组合框事件:

Private Sub cmbLocations_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbLocations.SelectedIndexChanged
    If Not cmbLocations.SelectedIndex = -1 Then
        If cmbLocations.SelectedIndex = cmbLocations.Items.Count - 1 Then
            If diaAddLocation.IsAccessible = False Then diaAddLocation.Activate()
            diaAddLocation.RequestSender = Me
            diaAddLocation.ShowDialog()
            FillLocations()
            cmbLocations.SelectedIndex = LocationFromLocationName(diaAddLocation.formresult)
            diaAddLocation.Close()
            diaAddLocation.Dispose()
        Else
            bttYes.Enabled = True
        End If
    End If
End Sub

子按钮按下和返回值函数

Public Sub bttAddLOCtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddLOCtoDatabase.Click

    Dim LocationToBeAdded As String
    LocationToBeAdded = "'" & TextBox1.Text & "'"
    AddLocation("'" & textbox1.Text & "'")
    FormResult = textbox1.Text
    GetLocations()
    frmFieldMaster.InitialiseNewParameter()
    Me.Hide()
End Sub


Public Function Result() As String
    Return FormResult
End Function

编辑:

实现了史蒂夫解决方案的代码:

Public Sub bttAddLOCtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddLOCtoDatabase.Click

    Dim LocationToBeAdded As String
    LocationToBeAdded = "'" & TextBox1.Text & "'"
    AddLocation("'" & textbox1.Text & "'")
    FormResult = textbox1.Text
    GetLocations()
    frmFieldMaster.InitialiseNewParameter()
    DialogResult = Windows.Forms.DialogResult.OK
    'me.Hide()

End Sub

Public Function Result() As String
    Return FormResult
    Me.Close()
End Function

Private Sub cmbLocations_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbLocations.SelectedIndexChanged
    Dim ValueTaken As Boolean = False
    If Not cmbLocations.SelectedIndex = -1 Then
        If cmbLocations.SelectedIndex = cmbLocations.Items.Count - 1 Then
            Using diaaddlocation = New diaAddLocation
                diaaddlocation.requestsender = Me
                If DialogResult.OK = diaaddlocation.showdialog Then
                    FillLocations()
                    cmbLocations.SelectedIndex = LocationFromLocationName(diaaddlocation.result)
                    diaaddlocation.close()
                ElseIf DialogResult.Cancel = diaaddlocation.showdialog Then
                    cmbLocations.SelectedIndex = -1
                End If
            End Using
        Else
            bttYes.Enabled = True
        End If
    End If
End Sub

当我运行代码时,它会输入IF DialogResult.OK... 并打开孩子。然后当我关闭孩子时,父母会运行接下来的两行代码并从孩子那里得到结果。在此之后,父母再次运行IF DialogResult.OK... 并在孩子打开时停止。代码永远不会到达diaaddlocation.close 行。

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    您不需要所有这些。你可以试试这样的

       If cmbLocations.SelectedIndex = cmbLocations.Items.Count - 1 Then
            Using diaAddLocation = new diaAddLocation()
                 diaAddLocation.RequestSender = Me
                 if DialogResult.OK = diaAddLocation.ShowDialog() then
                     FillLocations()
                     cmbLocations.SelectedIndex = LocationFromLocationName(diaAddLocation.formresult)
                 End If
            End Using
       Else
           .....
    

    这需要将bttAddLOCtoDatabaseDialogResult 属性设置为DialogResult.OK,并将子窗体AcceptButton 属性设置为bttAddLOCtoDatabase。现在您可以删除 bttAddLOCtoDatabase_Click 方法中的 Hide() 调用

    这是有效的,因为在您不退出 Using 语句之前,您的子表单仍然可以读取其属性(结果)

    编辑:与主要问题无关,但这些行是错误的:

     ElseIf DialogResult.Cancel = diaaddlocation.showdialog Then
           cmbLocations.SelectedIndex = -1
    

    你应该去

      Using diaAddLocation = new diaAddLocation()
          diaAddLocation.RequestSender = Me
          Dim dr = diaAddLocation.ShowDialog()
          if dr = DialogResult.OK then
              ....
          else if dr = DialogResult.Cancel then
              ....
          end if
    

    【讨论】:

    • 我已经尝试过实现这一点,但我仍然遇到类似的问题。在IF 条件下,结果会发送给父级,但在此过程中子级会关闭并重新打开。
    • 你在用 FillLocations() 做什么?你能显示那个函数的代码吗?如果您暂时删除对 FillLocations() 的调用,在 cmbLocations.SelectedIndex 上设置断点并检查 diaAddLocation.Results 的值,会发生什么情况?
    • LocationFromLocationName(diaaddlocation.result) 也可能参与表单的重新打开
    【解决方案2】:

    我不明白是什么问题,但如果你没有得到“FormResult”的价值

    如果你关闭没关系,但最好在关闭它之前设置 DialogResult,因为你将它显示为对话框(showdialog)

    验证 diaAddLocation 是你的窗口的实例,而不是直接的窗口类 如果您的表单名称是 frmdiaAddLocation,则不要像这样使用它

    frmdiaAddLocation.showdialog
    

    像这样使用它

    Dim diaAddLocation AS  frmdiaAddLocation = New frmdiaAddLocation()
    diaAddLocation.ShowDialog()
    

    只有这样使用才能为您提供结果价值

    【讨论】:

    • 我得到结果了,问题是得到结果是重新打开表格,我看看你说的有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 2021-04-16
    • 2011-08-30
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多