2011-05-20 86 views
0

在我的vb程序中,我有兩種形式一起工作,以允許您定義幾乎任意數量的設計。在第一種形式中,存在一個二維數組,存儲一個整數(不重要)和一個我以另一種形式定義爲公共內部類的ID類。點擊這個表單中的「定義ID」按鈕將帶你到下一個表單,在那裏你將定義一個新的ID或者編輯一個基於你在第一個表單的組合框中選擇或輸入的數組索引的舊ID,使用第二種形式的多個字段來定義其不同的方面。我想要發生的是第一種形式的代碼在用戶以第二種形式定義ID時暫停,然後他們單擊第二種形式上的「繼續」按鈕。經過一些錯誤檢查之後,表單要麼暫時隱藏(這是我最初實現它的方式),要麼返回編輯後的ID對象,但前提是錯誤檢查沒有指示任何輸入錯誤。最初,我只是通過使用newForm.curID獲取ID並將其設置爲數組中的位置,等待用戶使用newForm.showDialog()完成,但有時程序會返回空異常,因爲我沒有認爲我正確地複製了ID。此外,雖然錯誤顯示時間很短,但繼續按鈕會保存該ID,而不管其正確性如何。有沒有辦法操作showDialog以獲取結果響應呢?如何等待Visual Basic 2010窗體的按鈕單擊事件結束

簡短版本:我需要將一個內部類的對象以一種形式返回給調用表單中的數組,或等待表單完成並使用調用表單複製其對象,然後關閉被調用表單。

第一形式的 「定義」 按鈕方法

Private Sub DesignButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DesignButton.Click 
    Me.totalCoilBox.Focus() 'calls validating method, makes textbox yellow if invalid' 

    If totalCoilBox.BackColor = Color.White Then 
     Dim newForm As New IVD_DesignData 'creates an object of ID called curID as well' 

     Try 
      If DesignIDBox.Items.Contains(DesignIDBox.Text) Then 'design exists 
       set the curID to the one already in the array' 
       newForm.curID = CGID(Integer.Parse(DesignIDBox.Text), 1) 
       'set number of coils to new number, if applicable' 
       CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(DesignIDBox.Text) 

       'show dialog for editing' 
       newForm.ShowDialog() 
       'put the (edited) curID back into the array' 
       CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID 
      Else 'design does not exist, make a new one 
       put number of coils into new array spot' 

       CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text) 
       'set the design ID to the number of defined IDs plus one' 
       newForm.curID.designID = Integer.Parse(DesignIDBox.Text) 
       'show dialog for editing' 
       newForm.ShowDialog() 
       'add curID into the array' 
       CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID 
       'add that design number to the dropdown box' 
       DesignIDBox.Items.Add(newForm.curID.ToString()) 
       'increment number of defined designs' 
       Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1 
      End If 

     Catch ex As ArgumentOutOfRangeException 'dropdown box has no elements in it; 
      do the same as adding a new ID: 
      put number of coils into new array spot' 
      CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text) 
      'set the design ID to the number of defined IDs plus one' 
      newForm.curID.designID = Integer.Parse(DesignIDBox.Text) 
      'show dialog for editing' 
      newForm.showDialog() 
      'add curID into the array' 
      CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID 
      'add that design number to the dropdown box' 
      DesignIDBox.Items.Add(newForm.curID.ToString()) 
      'increment number of defined designs' 
      Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1 
     End Try 
     DesignIDBox.Text = newForm.curID.ToString() 
     newForm.Close() 
    End If 

第二形態的 「繼續」 按鈕方法

Private Sub ContinueButton_Click(ByVal sender As System.Object, ByVal e As ByVal e As System.EventArgs) Handles ContinueButton.Click 
    Me.NTBox.Focus() 
    Me.IDBox.Focus() 
    Me.ODBox.Focus() 
    Me.TBox.Focus() 
    Me.WBox.Focus() 
    If HSCBox.Checked Then 
     Me.HSC_MultBox.Focus() 
    Else 
     curID.HSC = "n" 
     curID.HSC_Mult = 1 
    End If 

    If NTBox.BackColor = Color.White And IDBox.BackColor = Color.White And ODBox.BackColor = Color.White And TBox.BackColor = Color.White And WBox.BackColor = Color.White And ((HSCBox.Checked And HSC_MultBox.BackColor = Color.White) Or Not (HSCBox.Checked)) Then 
     'success! window falls back?' 
    End If 
End Sub 

感謝任何及所有的幫助!我對Java更加熟練,所以這個VB項目有時候有點令人沮喪。

回答

0

關於第二個形式,使一個方法是這樣的:

public function ReturnSomething(curId as whateverType) as whateverReturnType 

    me.curId = curId 
    me.showDialog 
    return someValue 

end function 

然後調用,從你的第一種形式。

編輯:另外,讓你的第二個窗體的繼續方法調用Me.Close。這將關閉第二種形式。 ShowDialog調用會阻塞,直到表單關閉。

第一種形式不應該調用第二種形式的close方法。

+0

這是朝正確方向邁出的一步,但不幸的是,除非我誤解,否則在點擊繼續後,我的錯誤檢查仍然被忽略。 – Bret 2011-05-20 17:12:27

+0

看我的編輯,這應該也有幫助。 – 2011-05-20 18:14:30