2009-03-05 43 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