如何获取对象类型以便可以直接转换为它?这是我想执行的理想方法:
Dim MyObjects As New List(Of Object)
For Each O As Object In GlobalFunctions.GeneralFunctions.FindControlsRecursive(MyObjects, Form)
Select Case True
Case TypeOf O Is MenuStrip Or TypeOf O Is ToolStripButton Or TypeOf O Is Panel Or TypeOf O Is Label Or TypeOf O Is ToolStripSeparator
AddHandler DirectCast(O, O.GetType).Click, AddressOf GotFocus
End Select
Next
我试图使代码更高效,这样我就不必直接转换为指定的对象类型。前任。:
Dim MyObjectsAs New List(Of Object)
For Each O As Object In GlobalFunctions.GeneralFunctions.FindControlsRecursive(MyObjects, Form)
Select Case True
Case TypeOf O Is MenuStrip
AddHandler DirectCast(O, MenuStrip).Click, AddressOf GotFocus
Case TypeOf O Is Panel
AddHandler DirectCast(O, Panel).Click, AddressOf GotFocus
Case TypeOf O Is ToolStripButton
AddHandler DirectCast(O, ToolStripButton).Click, AddressOf GotFocus
Etc...
End Select
Next
编辑
据我所知,ToolStripItem
(ToolStripButton)不是 aControl
所以我不能List(Of Control)
在这种情况下使用 a 。当我第一次使用控件列表时,工具条项目不包括在内。这是我第一次在应用程序中使用,所以直到现在ToolStrip
我都没有理由不使用。List(Of Control)