?
Solved

Create new Instance of a Form by String Name

Posted on 2012-04-07
5
Medium Priority
?
499 Views
Last Modified: 2012-04-09
Experts,

I have a Form which has several components making up the UI.  The Form contains the following controls:

SplitContainer1 with Panel1 and Panel2
Panel1 has a ToolStrip for the Buttons I have
Panel2 of the SplitContainer has two Panel controls (One called Form and the other called Data)

When I click on the buttons in the ToolStrip I want the desired Form to load in Form Panel using a string name I provide which matches the Form Name.  The name of the form is placed in a Public Property defined as a string; p_sPanelFormName..

Panel Form will switch between the various Forms showing what I want with results placed in the Panel Data as needed.  I have the attached VB.NET code which appears to do what I want, but I get the error, "Object reference not set to an instance of an object.

Help please...  Thank you.

Code executed to update Panel Form with the new Form with its name stored in p_sPanelFormName:

        Dim f_PanelForm As Form = oclsObjects.fncCreateForm((p_sPanelFormName))
        f_PanelForm.TopLevel = False
        f_PanelForm.Parent = Me
        Panel_Form.Controls.Add(f_PanelForm)
        f_PanelForm.Show()

--------------------

Class containing Function to create the new instance of the Form I was to display in the Panel Form:

Imports System
Imports System.Windows.Forms
Imports System.Reflection

Public Class clsMod_CreateObjects

    '01.00.000  04/06/2012  04/06/2012  Initial release of this Function
    Public Function fncCreateObjectInstance(ByVal objectName As String) As Object

        ' Creates and returns an instance of any object in the assembly by its type name.
        Dim obj As Object
        Try
            If objectName.LastIndexOf(".") = -1 Then
                'Appends the root namespace if not specified.objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
            End If
            obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
        Catch ex As Exception
            obj = Nothing
        End Try
        Return obj

    End Function

    '01.00.000  04/06/2012  04/06/2012  Initial release of this Function
    Public Function fncCreateForm(ByVal formName As String) As Form

        ' Return the instance of the form by specifying its name.
        Return DirectCast(fncCreateObjectInstance(formName), Form)

    End Function

End Class

----------------------------

According to the first part of code I am not creating a new instance of the Form from the Public Property string.  I am not sure how to go about this.  Also, I need to update my code to show the proper Panel Form to the user which is contained in SplitContainer.
0
Comment
Question by:Peter Allen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
ID: 37819140
You are not creating any form from either of your functions (and probably the reason why you are getting the error)
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 37819660
Here is example code for creating a form from its name


Dim myAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim myForm As Form = TryCast(myAssembly.CreateInstance(txtFormName.text), Form)
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 800 total points
ID: 37819714
0
 

Author Comment

by:Peter Allen
ID: 37821228
nepaluz,

The first function shown creates the object.  The second function calles the first function and creates the Form, but I know I nead a New version of the form.  That is why I am getting the error.  That is what I need assistance for.


CodeCruiser,

Your example gives me the same error.  In this case if I add "New" in front of "Form =' I get "End of statement expected.

BuggyCoder, this example looks like it would work, but when I send the assembly name concatinating the form name from public property p_sName I get "value cannot be null parameter name type from the line, "     obj = Activator.CreateInstance(myType)" in the example.

I tried all three suggestions so far.  ...
0
 

Author Closing Comment

by:Peter Allen
ID: 37824704
After I wrote my reply I looked again at BuggyCoder's example.  It turns out I was missing a line of code and had a space/invalid character preventing the AssemblyName from being acquired.  Weird.  So I created a brand new project without any "questionable" code in the class or Forms.<br /><br />The following class worked perfectly:<br /><br />Imports System<br />Imports System.Reflection<br /><br /><br />Public Class clsMod_CreateObjects<br /><br />    '01.00.000  04/06/2012  04/06/2012  Initial release of this Function<br />    Public Shared Function CreateObjectInstance(ByVal objectName As String) As Object<br /><br />        ' Creates and returns an instance of any object in the assembly by its type name.<br />        Dim obj As Object<br />        Try<br />            If objectName.LastIndexOf(".") = -1 Then<br />                'Appends the root namespace if not specified.<br />                objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName<br />            End If<br />            obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)<br />        Catch ex As Exception<br />            obj = Nothing<br />        End Try<br />        Return obj<br /><br />    End Function<br /><br />    '01.00.000  04/06/2012  04/06/2012  Initial release of this Function<br />    Public Shared Function CreateForm(ByVal formName As String) As Form<br /><br />        ' Return the instance of the form by specifying its name.<br />        Return DirectCast(CreateObjectInstance(formName), Form)<br /><br />    End Function<br /><br />End Class
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question