vb.netdirectcast

How do I dynamically get an object type and cast to it?


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.


Solution

  • All controls derive from Control. Therefore, instead of using the type Object use Control. Control has most of the members of these controls like a Click event.

    Dim myControls As New List(Of Control)
    For Each ctrl As Control In _
      GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
    
        AddHandler ctrl.Click, AddressOf GotFocus
    Next
    

    Use Control in FindControlsRecursive as well.

    See:


    It turned out that you have some components not being controls. But you can still cast all controls to Control

    Dim myControls As New List(Of Object)
    For Each obj As Object In
            GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
    
        Select Case True
            Case TypeOf obj Is Control
                AddHandler DirectCast(obj, Control).Click, AddressOf GotFocus
            Case TypeOf obj Is ToolStripItem
                AddHandler DirectCast(obj, ToolStripItem).Click, AddressOf GotFocus
        End Select
    Next
    

    Note that ToolStripItem includes ToolStripButton, ToolStripControlHost, ToolStripDropDownItem, ToolStripLabel and ToolStripSeparator, since all of these components derive from ToolStripItem. You can see this in the Object Browser in Visual Studio: enter image description here

    MenuStrip is a Control. So, these two cases should cover most of your controls and components. If you find another component not covered here, search for its least derived base type featuring the Click event, so that the new case covers as many components as possible.