How to use reflection to open a form


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
My brain hurts a lot trying to figure this out. I would like to be able to have a property that defines the type of a form that I will open. I think I have that down:



Property ProjectSelectorType as System.Type

...

End Property



I can set the property with code like (UserContext is a static class, containing the ProjectSelectorType):



UserContext.ProjectSelectorType = GetType(myApp.ProjectSelectorForm)



So far so good. Now I need to open it. I tried this, which didn't work:



Dim frm As Object = Activator.CreateInstance(UserContext.ProjectSelectorType)

Dim mdiParentInfo As System.Reflection.PropertyInfo = UserContext.ProjectSelectorType.GetProperty("MdiParent")

mdiParentInfo.SetValue(frm, MicroFour.StrataFrame.Application.StrataFrameApplication.MainForm, Nothing)



' The next line throws an exception, my guess because Show() has two overloads

Dim showInfo As System.Reflection.MethodInfo = UserContext.ProjectSelectorType.GetMethod("Show")

showInfo.Invoke(frm)



As mentioned, it doesn't like the GetMethod("Show") call.



What am I doing wrong here? Is there a better way to do this?



I also tried interfaces, but I needed to pass an instance of the form implementing the interface. I don't want the form to be instantiated all the time, just when it's needed.



Thanks!



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