Iterate through all forms in a project...


Author
Message
StarkMike
StarkMike
Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)
Group: Forum Members
Posts: 436, Visits: 944
I can't seem to find out how to iterate through all the forms in a VB project. Why does this seem so difficult to do? I'm aware of the OpenForms property but I cant really open ALL the forms in my project just to iterate through them.



I can even find out how to reference a form if i have the name:

dim frm as form = Forms("Form1")



Anybody got any advice?
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Check out the My.Forms object (VB thingy). This should have a property for every form class defined in the current project. I have no idea how this would be used with C#.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Yeah, this confused me for a bit too.



You need to use reflection to use the name of the form to open it. I'm not very good at it yet. But I think this is heading in the right direction:



Dim myFormType As Type = Assembly.GetExecutingAssembly().GetType("myApp.myForm")

Dim frm As System.Windows.Forms.Form = DirectCast(Activator.CreateInstance(myFormType),System.Windows.Forms.Form)

frm.Show()





You might need to import System.Reflection for this code to compile.



There are methods to iterate over all of the methods or properties of a type given its name and also to call those methods/properties.



One last thing: if you intend to obfuscate your assembly, you can easily break any code using reflection. Specifically, if the name you form classes is changed, then the above code wouldn't work. You can tell the obfuscator not to rename a class (or properties or methods), so this isn't the end of the world. Just makes it harder.



StarkMike
StarkMike
Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)
Group: Forum Members
Posts: 436, Visits: 944
Thanks Greg! I appreciate the info, i just dont understand why it has to be so complicated! ;-) Everything is an object... you'd think there would be a collection somewhere that a person could use a simple for each loop against. Hehe
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Well, I think that its more complicated because everything isn't an object....until its been instantiated. Until then, it's a type (System.Type). I.e. you might have a form EmployeeForm. Really this is a Type, not an object. It's not an object until it's been instantiated. E.g. lets say you have a constructor that accepts an Employee ID, then loads that employees record into the form. Then you allow the user to open the form multiple times, for like five employees. Then you have 5 objects of the type EmployeeForm. Then you can iterate through them, using Application.OpenForms() to access these.



I'm still trying to figure out Reflection myself, so I'm still a bit confused as well. Also, since I need to obfuscate my assemblies, I'm more limited using reflection than I'd like.
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
If you are using an MDI environment then you can use the forms collection of the MDIClient as shown below.

Dim loForm As System.Windows.Forms.Form
Dim llReturn As Boolean = False
For Each loForm In MyMDI.MdiChildren
   If loForm.GetType() Is FormType
      llReturn = True
     
      '-- Place your code here
   End If   
Next
Return llReturn
End Function

Peter Jones
Peter Jones
Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)
Group: Forum Members
Posts: 386, Visits: 2.1K
Hi Guys,

This may (or not) help but we use DevExpress and I wanted a generic front end to all our DeVExpress reports that collects the data selection criteria for each report then instantiates the report and I didn't want to hard code anything. The starting point for this was to be able to find all DevExpress reports in a Solution. I couldn't figure out how to do this so I asked DevExpress support who supplied the following code.


    Private Sub CreateReportInstance(ByVal ReportName As String, ByRef Report As DevExpress.XtraReports.UI.XtraReport)

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

        ' Find all reports and assign the instance of the report name we are looking for
        For Each type As Type In asm.GetTypes()
            If type.IsSubclassOf(GetType(DevExpress.XtraReports.UI.XtraReport)) Then
                If (type.Name = ReportName) Then
                    Dim myReport As DevExpress.XtraReports.UI.XtraReport = CType(Activator.CreateInstance(type, False), DevExpress.XtraReports.UI.XtraReport)
                    Report = myReport
                    Exit For
                End If
            End If
        Next type
    End Sub


BTW - DevExpress and StrataFrame offer the best support it has ever been my pleasure to come across.

Cheers, Peter

StarkMike
StarkMike
Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)Advanced StrataFrame User (738 reputation)
Group: Forum Members
Posts: 436, Visits: 944
Thanks guys for all your input. WOW! Its been a BIG help.

P.S. Peter, I agree with you .... StrataFrame's TechSupport is Top Notch. Cool

StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
I'm not sure why the OpenForms property doesn't work for you... the OpenForms collection only contains instances of the forms that are currently open within the application.  So, when you first start the app and you only have the one form up, then the OpenForms collection is only going to contain that one form.  If you have 2 of the same type of form open, then the collection will contain the object references to both of those forms.  You shouldn't have to "open" any of the forms in the OpenForms collection because they're already open.

However, if you want to iterate through all of the form types in your application, then you'll need to do like Greg said and do a Me.GetType().Assembly.GetTypes() and test each type for IsSubclassOf() to get all of the form types in your app.

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search