views:

28

answers:

1

Hi,

I have an application with plugin-like structure. all application forms inherit from a base UserControl :

public class BaseUserControl : UserControl
{
// some common properties and methods
//
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
}
}

and are compiled in different assemblies.

When I want to show a form, it goes through this sequence:

assembly = Assembly.LoadFile(assemblypath);
.
.
frm = (BaseUserControl)assembly.CreateInstance(frmname);
.
.
SomeContainer.Controls.Add(frm);
MainScreen.Controls.Add(SomeContainer);

Common structure of these forms is :

public class TestForm : BaseUserControl {
    public TestForm(){InitializeComponent();}
    private void InitializeComponent(){
.
.
    this.Load += new System.EventHandler(this.TestForm_Load);
. 
   }
   private void TestForm_Load(object sender, EventArgs e){}

}

The problem is that the Load event of these forms does not get fired.

Another behavior, I dont understand how, when setting a breakpoint at the OnLoad in the base class, the call stack shows that is called from within the InitializeComponent.

Any ideas on how to solve this ?

Thanks in advance

+1  A: 

You are hopelessly mixing up the terms Form and UserControl, making it very hard to give a good answer. They are very different beasts, you can't turn a UC into a form. It is a client window, not a top-level window. I suspect that has something to do with your problem but the generic diagnostic is that somebody is overriding OnLoad and not calling base.OnLoad().

Yes, OnLoad may be called from code in InitializeComponent(). It isn't very healthy since OnLoad will run before the constructor is finished, but it is supported. This will happen when you touch a property that requires the Handle to be created. The call stack should show you which particular property assignment did this, just double-click the line in the call stack.

Hans Passant
Thank you for your answer. In fact the mixing of the terms form and usercontrol is due to translation from french; i mean by form the application screens, but technically, there is one main system.windows.forms.form and everything reside in usercontrols. The error happened with a module containing a web browser (parts of the app is already in asp.net), and the base usercontrol Load event was called exactly when that web browser was inserted in the controls collection, before creation of the event handler.
RMS