Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everybody, hope someone might help me with my problem.

How can i instantiate a Form from string variable using C#?

my scenarios are:

1. My application will populate its menu from a database
2. In that database, the menu has its reference to the form name
eg:
MenuDesc MenuObject // This refers to the column name
a. Data Entry - frmDataEntry // refers to the row data
b. Process Entry - frmProcessEntry

3. When I select/click Data Entry the system will Instantiate frmDataEntry. The process goes the same when i choose Process Entry.

Is this possible? Hope somebody out there might help me. I'm using C#
Posted

You can do the same with Activator.CreateInstance[^] method. Please read about Factory Method Pattern before implementing this.
 
Share this answer
 
You could store the full name of the form , (ex: foo.bar.frmDataEntry) , and use reflection to load the form object.


Let me know if you need more details

[EDIT] Did a google search and came up with what you want here

http://www.debugging.com/bug/8503[^]

what you need is

Method 3
	
'frmContractEdit needs to have Inherits System.Windows.Forms.Form in its Class definition (designer)
	Dim Form3Type As Type = Type.GetType("Fusion.frmContractEdit", True, True)
	MsgBox(Form3Type.AssemblyQualifiedName)
	Dim Form3 As Form = CType(Activator.CreateInstance(Form3Type), Form)
	Form3.Show()
'Method 4
	Dim Form4 As Form = CType(Activator.CreateInstance(Type.GetType("Fusion.frmContractEdit", True, True)), Form)
	Form4.Show()	
'Method 5
'Pass parameters using overload Activator.CreateInstance Object Paramater Array
'frmLegalServicesEdit(ByVal DD_TablesID As String, ByVal dgdBrow As DataGridView)
	Dim FType As Type = Type.GetType ("Fusion.frmLegalServicesEdit", True, True)
	Dim Args() As Object = {Me.DD_TablesID, Me.dgdBrow}
	Dim Form5 As Form = CType(Activator.CreateInstance(FType, Args), Form)
	Form5.Show()


I know its in VB .. but it should be straight forward to convert to C#
 
Share this answer
 
v2
Hi again.

This works when the form that you're calling/instantiating is within the same project. I'm having an error when i'm referencing to other project withing the same solution, that generates DLL when compiled.

my code:

// IBS_SysAd - a project that output to a DLL.
string sExec = "IBS_SysAd.frmMenu";

Type Form3Type = Type.GetType(sExec, true, true); // This is the point i'm encountering error. TypeLoadException, Could not load type 'IBS_SysAd.frmMenu' from assembly 'IBS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.


Form Form3 = (Form)Activator.CreateInstanceForm3Type);
Form3.Show();

tnx.
 
Share this answer
 
If it's in another assembly format the string this way: "TheNameSpace.TheType,OtherAssemblyName" & remember to put it in the same directory ;)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900