0

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

但是,将表单发送return value给父级并关闭表单的代码无法正常工作。我hide是孩子,然后与父母一起调用一个函数来访问return value. 此时子窗体和代码在运行另一个orshows之前停止。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行。

4

2 回答 2

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属性和bttAddLOCtoDatabase设置为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
于 2012-12-20T09:45:44.933 回答
0

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

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

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

frmdiaAddLocation.showdialog

像使用它一样

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

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

于 2012-12-20T10:49:19.300 回答