﻿<?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  » How to use reflection to open a form</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Wed, 10 Jun 2026 20:37:22 GMT</lastBuildDate><ttl>20</ttl><item><title>How to use reflection to open a form</title><link>http://forum.strataframe.net/FindPost6077.aspx</link><description>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:&lt;br&gt;
&lt;br&gt;
    Property ProjectSelectorType as System.Type&lt;br&gt;
      ...&lt;br&gt;
    End Property&lt;br&gt;
&lt;br&gt;
I can set the property with code like (UserContext is a static class, containing the ProjectSelectorType):&lt;br&gt;
&lt;br&gt;
    UserContext.ProjectSelectorType = GetType(myApp.ProjectSelectorForm)&lt;br&gt;
&lt;br&gt;
So far so good. Now I need to open it. I tried this, which didn't work:&lt;br&gt;
&lt;br&gt;
    Dim frm As Object = Activator.CreateInstance(UserContext.ProjectSelectorType)&lt;br&gt;
    Dim mdiParentInfo As System.Reflection.PropertyInfo = UserContext.ProjectSelectorType.GetProperty("MdiParent")&lt;br&gt;
    mdiParentInfo.SetValue(frm, MicroFour.StrataFrame.Application.StrataFrameApplication.MainForm, Nothing)&lt;br&gt;
&lt;br&gt;
    ' The next line throws an exception, my guess because Show() has two overloads&lt;br&gt;
    Dim showInfo As System.Reflection.MethodInfo = UserContext.ProjectSelectorType.GetMethod("Show")&lt;br&gt;
    showInfo.Invoke(frm)&lt;br&gt;
&lt;br&gt;
As mentioned, it doesn't like the GetMethod("Show") call. &lt;br&gt;
&lt;br&gt;
What am I doing wrong here?  Is there a better way to do this?&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
&lt;br&gt;</description><pubDate>Tue, 23 Jan 2007 10:59:34 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: How to use reflection to open a form</title><link>http://forum.strataframe.net/FindPost6094.aspx</link><description>[quote]How do you handle constructor parameters? I.e. I would normally do this:[/quote]&lt;/P&gt;&lt;P&gt;The Activator.CreateInstance supports parameters.&amp;nbsp; It accepts a ParamArray and will pass these to the constructor of the object being created.&amp;nbsp; You can create an overload on the LaunchForm method that accepts parms as well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [codesnippet]Private Sub LaunchForm(ByVal FormType As System.Type, ByVal ParamArray Parms() As Object)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Activator.CreateInstance(FormType, Parms)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub[/codesnippet]</description><pubDate>Tue, 23 Jan 2007 10:59:34 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: How to use reflection to open a form</title><link>http://forum.strataframe.net/FindPost6088.aspx</link><description>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 :D &lt;br&gt;
&lt;br&gt;
Now, the next question(s) (when haven't I had a next question ;)  ):&lt;br&gt;
&lt;br&gt;
- How do you handle constructor parameters?  I.e. I would normally do this:&lt;br&gt;
&lt;br&gt;
Dim frm As New MyForm(arg1, arg2)&lt;br&gt;
&lt;br&gt;
- 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:&lt;br&gt;
&lt;br&gt;
Dim frm As New MyForm(arg1, arg2)&lt;br&gt;
frm.MySpecialProperty = 8&lt;br&gt;
frm.SetAnotherSpecialProperty("some text")&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
&lt;br&gt;</description><pubDate>Tue, 23 Jan 2007 10:26:14 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: How to use reflection to open a form</title><link>http://forum.strataframe.net/FindPost6085.aspx</link><description>Generally we will create a method that looks something like this in the main MDI form:&lt;/P&gt;&lt;P&gt;[codesnippet]Private Sub LaunchForm(Byval FormType As System.Type)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Establish Locals&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim loForm As SYstem.Windows.Forms.Form&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Create the form&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; loForm = CType(Activator.CreateInstance(FormType), System.Windows.Forms.Form)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Set the MDI&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; loForm.MDIParent = Me&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Show the Form&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; loForm.Show()&lt;BR&gt;End Sub[/codesnippet]&lt;/P&gt;&lt;P&gt;Then to call or launch the form:&lt;/P&gt;&lt;P&gt;[codesnippet]LaunchForm(GetType(MyForm))[/codesnippet]</description><pubDate>Tue, 23 Jan 2007 09:49:43 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>