All Forms And Controls In An Application

  • Thread starter Thread starter RatFree
  • Start date Start date
R

RatFree

At run-time is there a way to get the names of all form classes and the
controls defined within each without instantiating the form classes?
I'm not doing any dynamic adding to the controls so the set of controls
added at design-time would be what I'm looking for.

If it can't be accomplished without instantiating then I do have code
to get all the controls from a given form class by instantiating that
form. What I'm having trouble with is getting all the form classes
within an application and instantiating each of those. The following
snippet will display the class name of each class defined in the
application, however I'm having trouble picking out only those that
inherit form and how to instantiate those that do.

Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()
For Each oType As Type In thisExe.GetTypes
MsgBox(oType.ToString)
Next

Thanks,
Bill
 
Well, I answered one part. The following snippet shows the names of
forms classes:

Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()
For Each oType As Type In thisExe.GetTypes
If oType.BaseType.ToString _
= "System.Windows.Forms.Form" Then
MsgBox(oType.ToString)
End If
Next

Now I'll see if I can instantiate them.
 
Now I have it solved with instantiation (yes, I tried for a long while
before I sent the original request but things seem to have fallen
together). The following snippet gives all the controls in all the
form classes:

Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()
For Each oType As Type In thisExe.GetTypes
If oType.BaseType.ToString _
= "System.Windows.Forms.Form" Then
MsgBox(Me.GetControlHierarchy( _
CType(Activator.CreateInstance(oType), Form)))
End If
Next

Private Function GetControlHierarchy( _
ByRef roForm As Form) As String
If roForm Is Nothing Then
Return "Form not instantiated."
End If

Dim sReturn As String = "FORM: " & roForm.ToString

For Each oControl As Control In roForm.Controls
sReturn &= vbNewLine & GetControlHierarchy(oControl, 1)
Next

Return sReturn
End Function

Private Function GetControlHierarchy( _
ByRef roControl As Control, _
ByVal riLevel As Integer) As String
If roControl Is Nothing Then
Return ""
End If

Dim sReturn As String
For iLevel As Integer = 1 To riLevel
sReturn &= "----> "
Next
sReturn &= "CONTROL: " & roControl.ToString

For Each oControl As Control In roControl.Controls
sReturn &= vbNewLine & _
GetControlHierarchy(oControl, riLevel + 1)
Next

Return sReturn
End Function
 
I'd still really like to know how to do this without instantiating the
forms. If anyone out there knows how to do this it would be great.
There's gotta be a way.
 
Back
Top