Thanks again Dave.
Ok....The architecture is not in my hands, though I can see why it has
been designed the way it has. I could explain it, but it would take a
few days!
You are correct about why I check DesignMode - I don't want someone
designing an app with the IDE to see error messages - this wouldn't
look very professional. However, in when I test the code within Visual
Studio, OnCreateControl doesn't fire when the control != visible. This
agrees with the symptoms that can be seen when running an app that has
been built by the "mini-IDE".
Also, the control does need to be a control and not a component.
Regards,
Greg.
Hi Greg,
> Ok....The architecture is not in my hands, though I can see why it has
> been designed the way it has. I could explain it, but it would take a
> few days!
Yea, no need :)
> You are correct about why I check DesignMode - I don't want someone
> designing an app with the IDE to see error messages - this wouldn't
> look very professional. However, in when I test the code within Visual
> Studio, OnCreateControl doesn't fire when the control != visible. This
> agrees with the symptoms that can be seen when running an app that has
> been built by the "mini-IDE".
In VS 2005 OnCreateControl is called when the containing Form is first opened
in the designer, even when Visible = false. This is because VS displays your
control even when it's not visible. I just tested it myself:
public class TestControl : Control
{
public TestControl()
{
BackColor = Color.Green;
Visible = false;
}
protected override void OnCreateControl()
{
MessageBox.Show("Created!");
base.OnCreateControl();
}
}
Add TestControl to any Form and see what I mean.
> Also, the control does need to be a control and not a component.
Here are a few options:
1. Place the code that needs to be executed in the constructor instead of
OnCreateControl.
2. Derive from UserControl and place code in the OnLoad method instead of
OnCreateControl.
3. Set BackColor to Color.Transparent and hide all child controls.

Signature
Dave Sexton
Greg - 25 Oct 2006 11:19 GMT
Thanks Dave.
Wow, what a nightmare!
Ok, I've looked into why it worked for you and not me. When you pressed
build and run, in the build stage, the control was been built, and then
added to the form. This is where your OnCreateControl was being called.
If you put a If(!DesignMode) test around the MessageBox call, the
message box never gets shown. Also, for the same reason, if you run the
app again without the build, the MessageBox never gets shown (even
without the DesignMode test).
So, OnCreateControl won't get called if the control is not visible.
However, I can see that the protected override void InitLayout() gets
called right at the end of the InitializeComponent() of the main form,
at which point controls can be interrogated as to whether there are in
DesignMode or not. InitLayout will therefore do what I need!
Thanks for your help - I've learned quite a bit on this one!
Regards,
Greg.
> Hi Greg,
>
[quoted text clipped - 41 lines]
> OnCreateControl.
> 3. Set BackColor to Color.Transparent and hide all child controls.
Dave Sexton - 25 Oct 2006 14:57 GMT
Hi Greg,
> Ok, I've looked into why it worked for you and not me. When you pressed
> build and run, in the build stage, the control was been built, and then
[quoted text clipped - 5 lines]
>
> So, OnCreateControl won't get called if the control is not visible.
OnCreateControl will get called even if the control is not visible, as I've
shown. It won't get called if the control is not built, but it must be built
to be added to the Form in the first place, so I'm not sure what you mean.
> However, I can see that the protected override void InitLayout() gets
> called right at the end of the InitializeComponent() of the main form,
> at which point controls can be interrogated as to whether there are in
> DesignMode or not. InitLayout will therefore do what I need!
Yes, it seems InitLayout is the perfect place for your code. From
Control.InitLayout on MSDN:
The InitLayout method is called immediately after adding a control to a
container
Good find!
> Thanks for your help - I've learned quite a bit on this one!
Glad to help :)

Signature
Dave Sexton