How do I get the object type so I can directly cast to it? This is the ideal method I would like to execute:
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
I am trying to make the code more efficient so that I do not have to directly cast to a specified object type. Ex.:
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
EDIT
To my knowledge, a ToolStripItem
(ToolStripButton) is not a Control
so I cannot use a List(Of Control)
for this situation. When I first was using a list of controls, the toolstrip items were not being included. This is the first time I have used ToolStrip
in an application so I never had a reason for not using List(Of Control)
until now.