我有一個父窗體與組合框填充從數據庫,用戶可以從中選擇。組合框中的最後一個值是「添加新的」,如果用戶選擇此選項,將打開一個子窗體供用戶向數據庫添加新值。我有一個按鈕按下事件將此值添加到數據庫,發送新的return value
到父級並關閉表單。然後父母應該從組合框中選擇新的值並等待用戶執行另一個操作。發送值給父母,然後關閉子窗體,在事件
但是,將return value
發送給父級並關閉表單的代碼無法正常工作。我hide
孩子,然後與父母調用一個函數訪問return value
。此時,孩子形成shows
,代碼在另一個hide
或close
之前停止。
我該如何解決這個問題(下面的代碼)?
父組合框事件:
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
行。
我試過實現這個,我仍然有類似的問題。在「IF」條件下,結果被髮送給父母,但在此過程中,孩子關閉並重新打開。 – Pezzzz
請參閱上面的編輯更多詳細信息... – Pezzzz
你在做什麼與FillLocations()?你能展示那個函數的代碼嗎?如果臨時刪除對FillLocations()的調用,通過cmbLocations.SelectedIndex設置斷點並檢查diaAddLocation.Results的值,會發生什麼情況? – Steve