Click here to Skip to main content
13,627,794 members
Rate this:
 
Please Sign up or sign in to vote.
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 9-Dec-13 5:36am
Comments
   
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?
   
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
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
AngelBlueSky 10-Dec-13 15:01pm
   
I agree with you completly, mine was just an example, I asked about mdi and child loaded runtime just to learn how to get events from them into a parent form using an interface. Anyway I created an interface, inherit it from the child and using the event now I can get events fired by the child. Thank again dude.
Anyway how would you implement an application having a lot of form?
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180712.1 | Last Updated 9 Dec 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100