﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » WinForms (How do I?)  » Creating an Instance of a Form From a String</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sat, 06 Jun 2026 03:55:41 GMT</lastBuildDate><ttl>20</ttl><item><title>Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22727.aspx</link><description>This is turning out to be a little more difficult than I expected.  I used the [url=http://forum.strataframe.net/Topic9345-14-1.aspx?Highlight=Type.GetType]forum entry[/url] to guide me but I have run into a problem with the following code:&lt;br&gt;
&lt;br&gt;
[codesnippet]((Form)Activator.CreateInstance(mFormName, "Form")).Show();[/codesnippet]&lt;br&gt;
&lt;br&gt;
The error that I get when I build the project is:&lt;br&gt;
&lt;br&gt;
[quote]Cannot convert type 'System.Runtime.Remoting.ObjectHandle' to 'System.Windows.Forms.Form'[/quote]&lt;br&gt;
&lt;br&gt;
I have tried simply passing the string of the form ("MyNamespace.MyForm") to CreateInstance, but that does not work, either.&lt;br&gt;
&lt;br&gt;
Any direction that you can provide would be helpful.&lt;br&gt;
Bill</description><pubDate>Wed, 15 Apr 2009 11:44:54 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22744.aspx</link><description>Hi Bill,&lt;br&gt;
Glad to be able to help.  I also borrowed some idea from your code to use the AssemblyQualifiedName and just type the form name like "frmInventory" in the Tool.Key property, in my case I always prefix all my forms with "frm", this will make my code more standard so I can use it other projects without having to do many Find/Replace changes.&lt;br&gt;
Here is the code I used in the UltraToolbarManager1_ToolClick() event:&lt;br&gt;
&lt;br&gt;
[codesnippet][code]&lt;br&gt;
        Dim ItemKeyStr As String = e.Tool.Key&lt;br&gt;
        If Not String.IsNullOrEmpty(ItemKeyStr) Then&lt;br&gt;
            If ItemKeyStr.StartsWith("frm", StringComparison.CurrentCultureIgnoreCase) Then&lt;br&gt;
                '-- If this is a form its name should start with "frm", so lets get the main form's AssemblyQualifiedName&lt;br&gt;
                '   and remove the [, Version=] string and replace its name with the ItemKeyStr value which should be the&lt;br&gt;
                '   form's name to be open.&lt;br&gt;
                Dim mOpenFullName As String = String.Empty&lt;br&gt;
                Dim AssemblyQualifiedName As String = Me.GetType().AssemblyQualifiedName&lt;br&gt;
                mOpenFullName = AssemblyQualifiedName.Remove(AssemblyQualifiedName.IndexOf(", Version=")).Replace(Me.Name, ItemKeyStr)&lt;br&gt;
                Me.LaunchDialog(Type.GetType(mOpenFullName))&lt;br&gt;
&lt;br&gt;
                ' Me.LaunchDialog(Type.GetType(ItemKeyStr))&lt;br&gt;
            Else&lt;br&gt;
                '-- Process all custom forms and options here.&lt;br&gt;
                Select Case e.Tool.Key&lt;br&gt;
                    Case "DatabaseConnection"    ' ButtonTool&lt;br&gt;
                        '-- Set the connections&lt;br&gt;
                        Me.SetDatabaseConnection()&lt;br&gt;
&lt;br&gt;
                        '-- TODO: close all child forms to force them to use the new connection string&lt;br&gt;
&lt;br&gt;
                    Case "SecurityMaintenanceForm"    ' ButtonTool&lt;br&gt;
                        Me.ShowSecurityDialog()&lt;br&gt;
&lt;br&gt;
                    Case "Exit"    ' ButtonTool&lt;br&gt;
                        Me.Close()&lt;br&gt;
&lt;br&gt;
                End Select&lt;br&gt;
            End If&lt;br&gt;
&lt;br&gt;
        End If&lt;br&gt;
[/code][/codesnippet]</description><pubDate>Wed, 15 Apr 2009 11:44:54 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22741.aspx</link><description>[quote]...one thing you can try also is to leave out the version in that string so if you are "hard coding" this and you change a version, it won't die.[/quote]&lt;br&gt;
&lt;br&gt;
Excellent!  I whacked everything from ", Version=" and following...the code works perfectly.  That will resolve that "hard coding" problem.  &lt;br&gt;
&lt;br&gt;
I enter the string "Aspire.Sales.CustomerInvoices, Sales" into the tag property of the NavBarItem, then run this code for the LinkClicked event:&lt;br&gt;
&lt;br&gt;
[codesnippet]&lt;br&gt;
private void OpenForm(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)&lt;br&gt;
{&lt;br&gt;
    String mFullName = (String)((DevExpress.XtraNavBar.NavBarItem)sender).Tag;&lt;br&gt;
    Boolean IsOpen = false;&lt;br&gt;
    foreach (Form mOpenForm in Application.OpenForms)&lt;br&gt;
    {&lt;br&gt;
        String mOpenFullName = mOpenForm.GetType().AssemblyQualifiedName;&lt;br&gt;
        mOpenFullName = mOpenFullName.Remove(mOpenFullName.IndexOf(", Version="));&lt;br&gt;
        if (mOpenFullName == mFullName)&lt;br&gt;
        {&lt;br&gt;
            IsOpen = true;&lt;br&gt;
            if (mOpenForm.WindowState == FormWindowState.Minimized) { mOpenForm.WindowState = FormWindowState.Normal; }&lt;br&gt;
            mOpenForm.Activate();&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
    if (!IsOpen) { (Activator.CreateInstance(Type.GetType(mFullName)) as Form).Show(); }&lt;br&gt;
}&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
I did add a tweak for the windowstate (thanks, Edhy!!).  All is well.&lt;br&gt;
&lt;br&gt;
Sighing contentedly,&lt;br&gt;
Bill</description><pubDate>Wed, 15 Apr 2009 09:42:40 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22740.aspx</link><description>[quote][b]Bill Cunnien (04/15/2009)[/b][hr]Edhy, I do need to review your code, since I'd rather not have to hard code the AssemblyQualifiedName in every LinkItem.  As soon as the assembly version ramps up, my NavBar is broken.  &lt;br&gt;
[/quote]&lt;br&gt;
Humm, I am using Infragistics's UltraToolbarManager and now that you mention it, this could also happen to me at any rate. Currently I am manually setting the Key property of each tool's item with a string like this: "CardTrackingSystem.frmInventory" and so far it works, I could also just enter the form's name "frmInventory" and have the LaunchDialog get the assembly info to avoid hard coding the full name all the time, but of course any way to automate the process and make it more reliable is always welcome, so I'll be watching this post for more info :)</description><pubDate>Wed, 15 Apr 2009 09:39:55 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22739.aspx</link><description>I place the AssemblyQualifiedName into the tag of the NavBarItem, then point the LinkClicked event to this code:&lt;br&gt;
&lt;br&gt;
[codesnippet]&lt;br&gt;
private void OpenForm(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)&lt;br&gt;
{&lt;br&gt;
    String mFullName = (String)((DevExpress.XtraNavBar.NavBarItem)sender).Tag;&lt;br&gt;
    Boolean IsOpen = false;&lt;br&gt;
    foreach (Form mOpenForm in Application.OpenForms)&lt;br&gt;
    {&lt;br&gt;
        String mOpenFullName = mOpenForm.GetType().AssemblyQualifiedName;&lt;br&gt;
        if (mOpenFullName == mFullName)&lt;br&gt;
        {&lt;br&gt;
            IsOpen = true;&lt;br&gt;
            mOpenForm.Activate();&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
    if (!IsOpen) { (Activator.CreateInstance(Type.GetType(mFullName)) as Form).Show(); }&lt;br&gt;
}&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
Since I am in full control of the assemblies and their versions, I think that this may be the best and easiest route to take.  I'll comment out this code and call it complete.&lt;br&gt;
&lt;br&gt;
Thanks!!&lt;br&gt;
Bill</description><pubDate>Wed, 15 Apr 2009 09:33:22 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22738.aspx</link><description>Yup...one thing you can try also is to leave out the version in that string so if you are "hard coding" this and you change a version, it won't die.</description><pubDate>Wed, 15 Apr 2009 09:31:31 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22737.aspx</link><description>The code I was using yesterday was the following:&lt;br&gt;
&lt;br&gt;
[codesnippet]String mFullName = mForm.GetType().AssemblyQualifiedName;[/codesnippet]&lt;br&gt;
&lt;br&gt;
That produces the following string:&lt;br&gt;
&lt;br&gt;
[quote]Aspire.Sales.CustomerInvoices, Sales, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null[/quote]&lt;br&gt;
&lt;br&gt;
If I use that, the form fires up perfectly.  I'll clean up my code and post what I did for future reference.&lt;br&gt;
&lt;br&gt;
Edhy, I do need to review your code, since I'd rather not have to hard code the AssemblyQualifiedName in every LinkItem.  As soon as the assembly version ramps up, my NavBar is broken.  &lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Bill&lt;br&gt;</description><pubDate>Wed, 15 Apr 2009 09:26:45 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22736.aspx</link><description>You may need to also include the assembly name and version as well if you are not pulling a class from the same assembly (and sometimes even if you are).</description><pubDate>Wed, 15 Apr 2009 09:21:11 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22735.aspx</link><description>Yup...I did that and the response was the same string that I used in the earlier post: "Aspire.Sales.CustomerInvoices".  I remember running this kind of script yesterday and getting something totally different, like "Aspire.Sale.CustomerInvoices, CustomerInvoices, 1.0.0.0, blah blah".  I'll see if I can get back to that kind of string.</description><pubDate>Wed, 15 Apr 2009 09:19:25 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22733.aspx</link><description>Hi Bill,&lt;br&gt;
I hard code the full form name on a property of the menu items and then call my version of a LaunchDialog method which is a collection of many versions here in the forums, I'll post it here so you can check it out:&lt;br&gt;
&lt;br&gt;
[codesnippet][code]&lt;br&gt;
   Private Sub LaunchDialog(ByVal dialogType As System.Type)&lt;br&gt;
        '-- All new forms are based on the following class:&lt;br&gt;
        '   CardTrackingSystem.UI.Windows.Forms.ApplicationBaseForm&lt;br&gt;
        Try&lt;br&gt;
            Dim newFormToBeLaunched As CardTrackingSystem.UI.Windows.Forms.ApplicationBaseForm&lt;br&gt;
            newFormToBeLaunched = CType(Activator.CreateInstance(dialogType), Form)&lt;br&gt;
&lt;br&gt;
            '-- Check if the form exist in the MDI collection and if so, bring it to the from.&lt;br&gt;
            For Each currentLoadedForm As Form In Me._MDIClient.Controls&lt;br&gt;
                If currentLoadedForm.Name.Equals(newFormToBeLaunched.Name, StringComparison.CurrentCultureIgnoreCase) Then&lt;br&gt;
                    '-- IF the form is minimized then restore it.&lt;br&gt;
                    If currentLoadedForm.WindowState = FormWindowState.Minimized Then&lt;br&gt;
                        currentLoadedForm.WindowState = FormWindowState.Normal&lt;br&gt;
                    End If&lt;br&gt;
&lt;br&gt;
                    '-- Dispose the newly created instance and use the current one&lt;br&gt;
                    newFormToBeLaunched.Dispose()&lt;br&gt;
                    newFormToBeLaunched = Nothing&lt;br&gt;
&lt;br&gt;
                    '-- Show the form on top of the others.&lt;br&gt;
                    currentLoadedForm.BringToFront()&lt;br&gt;
&lt;br&gt;
                    Exit Sub&lt;br&gt;
                End If&lt;br&gt;
            Next&lt;br&gt;
&lt;br&gt;
            '-- If the form was not found as part of the MDI control, then add it&lt;br&gt;
            AddFormHandlers(newFormToBeLaunched)&lt;br&gt;
&lt;br&gt;
            '-- Set the parent MDI&lt;br&gt;
            newFormToBeLaunched.MdiParent = Me&lt;br&gt;
&lt;br&gt;
            '-- Show the dialog&lt;br&gt;
            newFormToBeLaunched.Show()&lt;br&gt;
&lt;br&gt;
            '-- Update the Open Window Items&lt;br&gt;
            SetOpenWindowsItems(CType(newFormToBeLaunched, ApplicationBaseForm))&lt;br&gt;
&lt;br&gt;
        Catch ex As Exception&lt;br&gt;
            Me.ShowMessageByKey("FormNoAvailableYet", My.Application.Info.Title)&lt;br&gt;
        End Try&lt;br&gt;
&lt;br&gt;
    End Sub&lt;br&gt;
[/code][/codesnippet]</description><pubDate>Wed, 15 Apr 2009 09:18:15 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22730.aspx</link><description>If you are going to create it off of a string, you have to pass the full name, not just the name of the class.&amp;nbsp; This can be a long discussion in and of itself, but in super Reader's Digest break down, and to save you some time, just create an instance of your object and then pull the full name off of it so you can see what it is:&lt;/P&gt;&lt;P&gt;[codesnippet]MyObject obj = new MyObject();&lt;/P&gt;&lt;P&gt;MessageBox.Show(obj.GetType().FullName);[/codesnippet]</description><pubDate>Wed, 15 Apr 2009 09:09:15 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Creating an Instance of a Form From a String</title><link>http://forum.strataframe.net/FindPost22729.aspx</link><description>By the way, if I change the code to this:&lt;br&gt;
&lt;br&gt;
[codesnippet](Activator.CreateInstance(Type.GetType("Aspire.Sales.CustomerInvoices")) as Form).Show(); [/codesnippet]&lt;br&gt;
&lt;br&gt;
I get the following error:&lt;br&gt;
&lt;br&gt;
[quote]Value cannot be null.\rParameter name: type[/quote]&lt;br&gt;
&lt;br&gt;
Obviously, the Type.GetType() function is not returning any type.  I have confirmed the string.  And, I have used several variations.  No dice.&lt;br&gt;
&lt;br&gt;
I started working on this yesterday afternoon.  I am stumped.  I had hoped a good night's sleep would provide a better perspective, but, alas!, it has not.&lt;br&gt;
&lt;br&gt;
Bill&lt;br&gt;</description><pubDate>Wed, 15 Apr 2009 09:07:29 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item></channel></rss>