Here I will post problems I and my colleagues met and solutions we found.

Wednesday, March 02, 2005

DesignMode and LicenseManager.UsageMode in constructor and Event handlers

Today I found why my forms behaved weirdly in design-time.

I have two forms, one is parent in terms of inheritance, another is child one. Example:

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// DesignMode = false if you open ChildForm
MessageBox.Show("Form1 constructor DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == DesignMode
MessageBox.Show("Form1 constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}


private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}


private void Form1_Load(object sender, System.EventArgs e)
{
// DesignMode = true if you open ChildForm
MessageBox.Show("Form1_Load DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == RunTime
MessageBox.Show("Form1_Load constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}

}


public class ChildForm : DesignModeExample.Form1
{
public ChildForm()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
}}

If you open Child form in design time you will see two dialog. One will say that DesignMode in Form1 constructor = false. Another that DesignMode in Load event = true. LicenseManager.UsageMode works opposite. It works properly in constructor but lies in Load event handler.

Why is it so? I don't know, but I just tested it. It works this way.

No comments: