Dynamically loading Windows Form
Description
Suppose you have a MDI windows application and there is a main menu at the top for loading forms and other objects etc.
if you know the name of the form then by using following code you can load the form in the parent form.
private void ParentForm_Load(object sender, EventArgs e)
{
Form f = GetForm("namespace.subdirectory1.subdirectory2...subdirectoryn.FormName");
f.MdiParent = this;
f.Show();
}
public Form GetForm(string FormName)
{
if (!String.IsNullOrEmpty(FormName))
return (Form)Activator.CreateInstance(Type.GetType(FormName));
return null;
}
the code is helpful if you already have stored form names in some table etc
one thing more --> prior using this code you must include following namespace.
using System.Reflection;
happy surfing
Regards
Aneela
Reference: http://aspdotnetcode.source-of-humor.com/CodeSnippets/WindowsFormApplication/Dynamically-loading-Windows-MDI-Child-Forms-at-runtime.aspx
Hi Friend,
When i was used following code in my application, i got Error.
"return (Form)Activator.CreateInstance(Type.GetType(FormName));"
the error was followed....
"System.ArgumentNullException was unhandled
Message="Value cannot be null.\r\nParameter name: type"
Source="mscorlib"
ParamName="type"
StackTrace:
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at DynamicMenuWinApp.DynamicMenuMDIParent.DynamicallyLoadedObject(String objectName) in C:\NITIN\PROJECTS\DynamicMenu\DynamicMenuWinApp\DynamicMenuWinApp\DynamicMenuMDIParent.cs:line 148
at DynamicMenuWinApp.DynamicMenuMDIParent.SelectedChildMenu_OnClick(Object sender, EventArgs e) in C:\NITIN\PROJECTS\DynamicMenu\DynamicMenuWinApp\DynamicMenuWinApp\DynamicMenuMDIParent.cs:line 131
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DynamicMenuWinApp.Program.Main() in C:\NITIN\PROJECTS\DynamicMenu\DynamicMenuWinApp\DynamicMenuWinApp\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
"
if any one knows, how to solve this one.....then please reply me immediately.