2012-12-20 70 views
0

我有一個父窗體與組合框填充從數據庫,用戶可以從中選擇。組合框中的最後一個值是「添加新的」,如果用戶選擇此選項,將打開一個子窗體供用戶向數據庫添加新值。我有一個按鈕按下事件將此值添加到數據庫,發送新的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行。

回答

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

這工作,因爲裏面,直到你不退出使用的語句,你的孩子形式仍然可以讀取它的屬性(結果)

編輯:不相關的主要問題,但這些線是錯誤的:

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 
+0

我試過實現這個,我仍然有類似的問題。在「IF」條件下,結果被髮送給父母,但在此過程中,孩子關閉並重新打開。 – Pezzzz

+0

請參閱上面的編輯更多詳細信息... – Pezzzz

+0

你在做什麼與FillLocations()?你能展示那個函數的代碼嗎?如果臨時刪除對FillLocations()的調用,通過cmbLocations.SelectedIndex設置斷點並檢查diaAddLocation.Results的值,會發生什麼情況? – Steve

0

去,我不明白什麼是問題b UT如果你沒有得到「FormResult」的價值

如果從關閉它並不重要,但它會更好關閉之前設置的DialogResult,因爲你正在展示它作爲對話框(的ShowDialog)

驗證diaAddLocation是你實例窗口沒有窗口類直接 如果你的名字形式frmdiaAddLocation那麼就不要使用它像

frmdiaAddLocation.showdialog 

使用它像

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

只能用這樣的方式才能爲你提供結果值

+0

我得到的結果,問題是得到的結果是重新打開窗體,我會看看你說什麼和看看它是否有幫助。 – Pezzzz

+0

不要隱藏它 –