2009-03-05 21 views
0

我在運行時創建了一個帶有幾個按鈕和一個組合框的窗體。如何捕獲運行時對象上的事件

dim f as new form 

(等等等等)

然後按鈕acceptDescription和rejectDescription都設置...
然後組合框descriptionCombo設置...

然後...

AddHandler acceptDescription.Click, AddressOf handleAcceptedDescription 
AddHandler rejectDescription.Click, AddressOf handleRejectedDescription 

然後我有這兩種方法來捕捉點擊事件...但無法弄清楚如何引用其他運行時生成d控件。 (如果被接受,則爲組合框,如果被拒絕則表格)

Private Sub handleAcceptedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    'stub 
    'will need to reference the chosen combobox value here 
    dim acceptedDescription as string = descriptionCombo.selectedValue .tostring 
End Sub 
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    'I want to close the runtime created form here, but can't reference it 
    f.close() 
    'and return user to main form 
    Me.Focus() 
End Sub 

回答

0

爲了避免全球性的定義,最好的回答是

Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    'sender is a button in this case. 
    'get the button 
    dim b as new button 
    b = ctype(sender,button) 
    'now get the button's parent form 
    dim f as new form 
    f = ctype(b.parent, form) 
    'now close the form 
    f.close() 
End Sub 
0

爲什麼不能引用它?只需將其保存爲模塊/表單級別的變量,然後設置即可。

1

如果用於生成表單的代碼位於主表單中,則可以在主表單類的類級別聲明Form變量,以便您可以從事件處理程序中訪問它。您的組合框和文本字段也是如此 - 您需要確保變量在處理程序範圍之外聲明,以便您可以在處理程序中引用它們。

+0

這應該工作 - 但我想,它必須能夠沒有宣佈更多的全局變量。 謝謝。 – m42 2009-03-05 16:53:54