在事件发送值给父母,然后关闭子表单

时间:2012-12-20 09:24:01

标签: vb.net winforms

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

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

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

Parent Combobox事件:

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行。

2 个答案:

答案 0 :(得分: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
       .....

这需要DialogResult的{​​{1}}属性设置为bttAddLOCtoDatabase,子格式DialogResult.OK属性设置为AcceptButton。现在,您可以删除bttAddLOCtoDatabase方法

中的Hide()调用

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

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

bttAddLOCtoDatabase_Click

你应该去

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

答案 1 :(得分:0)

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

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

验证diaAddLocation是否是窗口的实例而不是窗口类 如果您的表单是frmdiaAddLocation,那么请不要像

那样使用它
frmdiaAddLocation.showdialog

一样使用它
Dim diaAddLocation AS  frmdiaAddLocation = New frmdiaAddLocation()
diaAddLocation.ShowDialog()

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