Saturday 26 March 2016

Open form with Form Name in winform appliaction


When we have only the form name and need to show / open the particular form after we clicked on a button or a menu strip is dynamically created, we need some dynamic code to handle the situation.

Here is the solution to open the particular form dynamically,



Code:
string  _formName ="MyForm";
System.ReflectionAssembly _asm = System.Reflection.Assembly.GetExecutingAssembly();
var _frm = (Form)(_asm.CreateInstance(this.GetType().Namespace+"."+ _formName));
_frm.Show();



Or

Code:
 var _frm = (Form)Activator.CreateInstance(Type.GetType(this.GetType().Namespace+"."+  _formName));
_frm.Show();

Thanks,