Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I made an mdi program and I load dinamically some child form this way:

Assembly newDll = Assembly.LoadFile("Child_01.exe");
Type type = newDll.GetType("Child.Form1");

Object obj = Activator.CreateInstance(type);
Form child = (Form)Activator.CreateInstance(type);

child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();


In the child forms I would like to send event (for example pushing a button etc)
to the Parent Form.

I tried using an interface but I still can't make the Parent Form to get the rising of the event.

here's the interface example:

namespace EventManager
{
public interface IEventInterface
{
event EventHandler<testargs> TestChanged;
void RaiseEvent(String value);
}

public class TestChangeArgs : EventArgs
{
public TestChangeArgs(String value)
{
ChangeText = value;
}

public String ChangeText { get; private set; }
}
}

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 11:57am    
You are trying to do relatively advanced things. This depth (even if you have some problems, let's set them aside for now) is in the great contrast with such a lame as MDI. Do you really want to torture yourself and scare off your users with this unfortunate design element? Even Microsoft takes measure to phase it out and at least discourage. Listen to a good friendly advice: never ever use MDI. You can do much better (and often much easier at the same time)...
—SA
AngelBlueSky 9-Dec-13 14:04pm    
You could be right but I can't include all forms as reference as I do with the DockPanel Suite. I need to write an application with a lot of forms. What do you suggest?
Sergey Alexandrovich Kryukov 9-Dec-13 15:10pm    
What do you mean "include all forms as reference"? Yes, you can have different options which a much better. Dock Panel suite (which one did you use), TabControl. It depends on expected complexity of your application. I see no reasons at all to keep to MDI...
—SA
AngelBlueSky 10-Dec-13 9:33am    
Hi I'll explain with an example project:

I use dockpanel suite to load some new tab, they're dinamically
loaded from external forms (dll or exe) like this:

private void cccToolStripMenuItem_Click(object sender, EventArgs e)
{
Assembly newDll = Assembly.LoadFile(Application.StartupPath + @"\002.exe");
Type type = newDll.GetType("_002.Form1");
object obj = Activator.CreateInstance(type);
Form frm1 = obj as Form;
frm1.TopLevel = false;

DockContent dockContent = new DockContent();
frm1.TopLevel = false;
frm1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frm1.Dock = DockStyle.Left;
frm1.Visible = true;
dockContent.ShowHint = WeifenLuo.WinFormsUI.Docking.DockState.DockLeftAutoHide;
dockContent.Text = frm1.Text;
dockContent.Controls.Add(frm1);
dockContent.Show(dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockLeft);
}

I'm doig like this because I've got a lot of them.

The question is: How can I get some event from the dinamically loaded form
into the main (parent) form?

Thanks again
Sergey Alexandrovich Kryukov 10-Dec-13 11:09am    
First of all, you are doing a nasty thing by finding a form type by name. Is it really necessary? Where do you get this name?
The best approach is to define some plug-in interface and see if a type implements it (IsAssignableTo...). Now, another problem: you load by Assembly.LoadFile. Do you even need to unload it? If you load more and more assemblies, you would get a memory leak.

Now, is it what you call MDI, your docking system? It does not look quite like MDI. Any link?
—SA

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