For a UserControl there is the 'Load event you can exploit to make sure the
internal structure of your control that inherits from a built-in WinForms
control is setup properly, most likely based on the user's changing the public
properties you have exposed to the IDE designer.
But for a control class that inherits from a built-in WinForms control, there is
no Load event exposed.
If you make a "compound" control : something where the Class inherits from a
built-in Windows Form UI control like :
public class ExtendedListView : System.Windows.Forms.ListView
{
// yada ... yada ...
}
And instantiate it, and set its properties, size it, etc. only from code : no
problems.
But when you go to the trouble of putting a ToolBox icon into it, and then
mounting it in the ToolBox where you want to select, drag, and drop it on
various container objects :
1. The constructor will be called twice : once at the moment of dropping it
on the container : again at the moment you 'run' the form
or project that instantiates and activates the container.
I've experimented with a variety of techniques (found here, on CodeProject,
etc.) that are designed to detect the difference between run-time and
design-time : these don't appear to work in this scenario.
I've tried the following types of coding techniques to no avail :
// doesn't work
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Form parentForm = this.FindForm();
if(parentForm != null)
{
if(parentForm.IsHandleCreated)
{
CreateUI();
}
}
}
// doesn't work
if(Parent != null && ! Parent.IsDisposed)
{
if(Parent.IsHandleCreated)
{
CreateUI();
}
}
// doesn't work
if(this.IsHandleCreated)
{
CreateUI();
}
// doesn't work
if(this.Site != null)
{
if(this.Site.DesignMode == false)
{
CreateUI();
}
}
Appreciate your advice !
Bill Woodruff
dotScience
Chiang Mai, Thailand
joeycalisay - 29 Nov 2004 03:55 GMT
> But for a control class that inherits from a built-in WinForms control, there is
> no Load event exposed.
They have the OnCreateControl counterpart.
> 1. The constructor will be called twice : once at the moment of dropping it
> on the container : again at the moment you 'run' the form
> or project that instantiates and activates the container.
true, the designer obviously instantiates the control in memory to be able
to simulate it at design time.
> I've experimented with a variety of techniques (found here, on CodeProject,
> etc.) that are designed to detect the difference between run-time and
> design-time : these don't appear to work in this scenario.
basically, what are you trying to do, DesignMode property is sufficient but
it need not be tested in constructor or a method called in constructor (even
on form level) since the controls aren't sited yet.
Bill Woodruff - 29 Nov 2004 07:08 GMT
I have found, through experimentation that if the Site property of the control
is null in either OnCreateControl or OnHandleCreated over-rides in your control
inherited from a Windows Forms control built-in, that you can assume the
control's host is in run-time mode.
I should say that "I believe I have found" since I am sure other revelations are
around the corner :) I believe I had tried looking at the Site property in the
constructor without success, but I am going to re-check that.
best, Bill Woodruff
dotScience
Chiang Mai, Thailand
Here's working code that illustrates this :
protected override void OnCreateControl()
{
base.OnCreateControl();
if(this.Site != null)
{
MessageBox.Show("in OnCreateControl override this.site.designmode = " +
this.Site.DesignMode.ToString());
}
else
{
MessageBox.Show("in OnHandleCreated override : site is null");
}
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if(this.Site != null)
{
MessageBox.Show("in OnHandleCreated override this.site.designmode = " +
this.Site.DesignMode.ToString());
}
else
{
MessageBox.Show("in OnHandleCreated override : site is null");
}
}
Bob Powell [MVP] - 29 Nov 2004 08:54 GMT
I think that the Site, like the DesignMode property are set by the designer
*after* the control is constructed. You won't be able to use either of these
in the constructor.

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> I have found, through experimentation that if the Site property of the control
> is null in either OnCreateControl or OnHandleCreated over-rides in your control
[quoted text clipped - 38 lines]
> }
> }