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?
Reply
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.



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