Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
normaly this is how i open a form, with 2 parameter

C#
FmCalculate cc = new FmCalculate("456", "123");
FmCalculate.ShowDialog();


now how can i open the form by using the string value
for example:
C#
String strFormName = "FmCalculate";

i tried below example, but it said the value is null, and how to include the "456" and "123" parameter?
C#
Form form = (Form)Activator.CreateInstance(Type.GetType("FmCalculate"));
form.ShowDialog();
Posted

1 solution

You should provide a fully qualified name type, including your namespace. That's why you're getting a null. For example:

C#
Form form = (Form)Activator.CreateInstance(Type.GetType("MyNameSpaceName.FmCalculate"));


And you should be able to pass any constructor parameters directly like:

C#
Form form = (Form)Activator.CreateInstance(Type.GetType("MyNameSpaceName.FmCalculate","456", "123"));


Also, if you're using a .NET version above 2 (if I remember correctly) you should be able to use the generic function call Activator.CreateInstance<t>()</t>:

C#
FmCalculate form = (FmCalculate)Activator.CreateInstance("456", "123"));


I'm going by memory here, so there may be some small inaccuracy. :-)
Please check the MSDN page on Activator.CreateInstance() for more details.
 
Share this answer
 
Comments
melvintcs 4-Aug-13 11:02am    
A big thanks :DDDDDDDD
Moreno Airoldi 4-Aug-13 14:36pm    
My pleasure. :-)

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