StrataFrame Forum

How to use reflection to open a form

http://forum.strataframe.net/Topic6077.aspx

By Greg McGuffey - 1/22/2007

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!


By Trent L. Taylor - 1/23/2007

Generally we will create a method that looks something like this in the main MDI form:

Private Sub LaunchForm(Byval FormType As System.Type)
    '-- Establish Locals
    Dim loForm As SYstem.Windows.Forms.Form

    '-- Create the form
    loForm = CType(Activator.CreateInstance(FormType), System.Windows.Forms.Form)

    '-- Set the MDI
    loForm.MDIParent = Me

    '-- Show the Form
    loForm.Show()
End Sub

Then to call or launch the form:

LaunchForm(GetType(MyForm))
By Greg McGuffey - 1/23/2007

Thanks! It was the casting that I wasn't getting. Cast it to a form and then you have access to MDIParent and Show...got it BigGrin



Now, the next question(s) (when haven't I had a next question Wink ):



- How do you handle constructor parameters? I.e. I would normally do this:



Dim frm As New MyForm(arg1, arg2)



- What about setting custom properties or calling custom methods? I normally try to keep an configuration to the constructor, but occassionally, that doesn't work (signatures are the same). I.e. how to handle:



Dim frm As New MyForm(arg1, arg2)

frm.MySpecialProperty = 8

frm.SetAnotherSpecialProperty("some text")



Thanks!


By Trent L. Taylor - 1/23/2007

How do you handle constructor parameters? I.e. I would normally do this:

The Activator.CreateInstance supports parameters.  It accepts a ParamArray and will pass these to the constructor of the object being created.  You can create an overload on the LaunchForm method that accepts parms as well:

   

Private Sub LaunchForm(ByVal FormType As System.Type, ByVal ParamArray Parms() As Object)
        Activator.CreateInstance(FormType, Parms)
    End Sub