﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Iterate through all forms in a project...</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 09 Jun 2026 08:23:54 GMT</lastBuildDate><ttl>20</ttl><item><title>Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8063.aspx</link><description>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.&lt;br&gt;
&lt;br&gt;
I can even find out how to reference a form if i have the name:&lt;br&gt;
     dim frm as form = Forms("Form1")&lt;br&gt;
&lt;br&gt;
Anybody got any advice?</description><pubDate>Thu, 12 Apr 2007 09:22:15 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8093.aspx</link><description>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.&amp;nbsp; 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.&amp;nbsp; If you have 2 of the same type of form open, then the collection will contain the object references to both of those forms.&amp;nbsp; You shouldn't have to "open" any of the forms in the OpenForms collection because they're already open.&lt;/P&gt;&lt;P&gt;However, if you want to iterate through all of the form &lt;EM&gt;types&lt;/EM&gt; 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.</description><pubDate>Thu, 12 Apr 2007 09:22:15 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8084.aspx</link><description>Thanks guys for all your input. WOW! Its been a BIG help.&lt;P&gt;P.S. Peter, I agree with you .... StrataFrame's TechSupport is Top Notch. :cool:</description><pubDate>Thu, 12 Apr 2007 06:57:58 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8078.aspx</link><description>Hi Guys,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub CreateReportInstance(ByVal ReportName As String, ByRef Report As DevExpress.XtraReports.UI.XtraReport)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Find all reports and assign the instance of the report name we are looking for&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For Each type As Type In asm.GetTypes()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If type.IsSubclassOf(GetType(DevExpress.XtraReports.UI.XtraReport)) Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If (type.Name = ReportName) Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim myReport As DevExpress.XtraReports.UI.XtraReport = CType(Activator.CreateInstance(type, False), DevExpress.XtraReports.UI.XtraReport)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report = myReport&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exit For&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next type&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/P&gt;&lt;P&gt;&lt;BR&gt;BTW - DevExpress and StrataFrame offer the best support it has ever been my pleasure to come across.&lt;/P&gt;&lt;P&gt;Cheers, Peter</description><pubDate>Wed, 11 Apr 2007 18:52:15 GMT</pubDate><dc:creator>Peter Jones</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8071.aspx</link><description>If you are using an MDI environment then you can use the forms collection of the MDIClient as shown below.&lt;/P&gt;&lt;P&gt;[codesnippet]Dim loForm As System.Windows.Forms.Form&lt;BR&gt;Dim llReturn As Boolean = False&lt;BR&gt;For Each loForm In MyMDI.MdiChildren&lt;BR&gt;&amp;nbsp;&amp;nbsp; If loForm.GetType() Is FormType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; llReturn = True&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Place your code here&lt;BR&gt;&amp;nbsp;&amp;nbsp; End If&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;Next&lt;BR&gt;Return llReturn&lt;BR&gt;End Function[/codesnippet]</description><pubDate>Wed, 11 Apr 2007 17:37:08 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8070.aspx</link><description>Well, I think that its more complicated because everything [i]isn't[/i] 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.&lt;br&gt;
&lt;br&gt;
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.</description><pubDate>Wed, 11 Apr 2007 17:24:34 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8066.aspx</link><description>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:</description><pubDate>Wed, 11 Apr 2007 15:43:39 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8065.aspx</link><description>Yeah, this confused me for a bit too.  &lt;br&gt;
&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
[codesnippet]Dim myFormType As Type = Assembly.GetExecutingAssembly().GetType("myApp.myForm")&lt;br&gt;
Dim frm As System.Windows.Forms.Form = DirectCast(Activator.CreateInstance(myFormType),System.Windows.Forms.Form)&lt;br&gt;
frm.Show()&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
You might need to import System.Reflection for this code to compile.  &lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;</description><pubDate>Wed, 11 Apr 2007 15:32:50 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Iterate through all forms in a project...</title><link>http://forum.strataframe.net/FindPost8064.aspx</link><description>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#.</description><pubDate>Wed, 11 Apr 2007 15:22:27 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>