What is the next event to fire after the OnCreateControl?

G

Greg

I am designing an inherited control. Besides the OnCreateControl , what
other events can be listened to that the if(!DesignMode) test willl
work? The reason I am asking is that a customer has asked to hide the
control. However, setting the controls visible property to false means
that the OnCreateControl which contains some essential processing never
fires. I need somewhere else to relocate this code, presumably in the
next event after the OnCreateControl event. Also, is there is a
published list of events in the lifecyle of a winforms control? I
can't find anything on this!
 
D

Dave Sexton

Hi Greg,

Once you make the control visible by setting Visible = true or calling Show(),
OnCreateControl will be called, but only the first time it becomes visible.

The "essential processing" probably shouldn't run unless the control is going
to be used (i.e., unless Visible = true), otherwise it probably shouldn't be
in the control to begin with.
 
G

Greg

Thanks Dave.

I know it is an odd request. Basically the control is one of many that
fit into a mini IDE that my company produces. A customer has requested
that the control works even when invisible (it sends data across a
network), since the operator using the app that the IDE creates never
needs to see or interact with the control. Due to the nature of the
project, the code that needs to be run does need to be in this control,
so I'm left wondering what my options are. It is possible to ask the
customer to place a panel control over the control to be hidden. Its
too much of a kludge in my opinion though. I'd rather do something
else, even if it is making the control visible for a moment, and then
re-hiding it (again a kludge!).

Thanks again,

Greg.
 
D

Dave Sexton

Hi Greg,

That's what Components are for. They don't display an interface :)

Just because the Control needs to perform some required processing doesn't
mean the actual processing should take place in the control. A better design
pattern would be to create a business object that can handle the custom
processing and simply invoke it from the control. This will allow you to make
a Control and Component that do the same thing. The customer can then choose
between the two.

I assume that you are worried about checking DesignMode because you are
getting errors in the IDE when your control is placed on a Form, however
OnCreateControl is called even when Visible=false in the IDE, AFAIK.
 
G

Greg

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.
 
D

Dave Sexton

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.
 
G

Greg

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.
 
D

Dave Sexton

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
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.

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top