Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

What is the next event to fire after the OnCreateControl?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 24 Oct 2006 14:54 GMT
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!
Dave Sexton - 24 Oct 2006 16:49 GMT
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.

Signature

Dave Sexton

>I am designing an inherited control. Besides the OnCreateControl , what
> other events can be listened to that the if(!DesignMode) test willl
[quoted text clipped - 5 lines]
> published list of events in the lifecyle of a winforms control? I
> can't find anything on this!
Greg - 24 Oct 2006 17:06 GMT
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.

> Hi Greg,
>
[quoted text clipped - 17 lines]
> > published list of events in the lifecyle of a winforms control? I
> > can't find anything on this!
Dave Sexton - 24 Oct 2006 17:16 GMT
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.

Signature

Dave Sexton

> Thanks Dave.
>
[quoted text clipped - 38 lines]
>> > published list of events in the lifecyle of a winforms control? I
>> > can't find anything on this!
Greg - 24 Oct 2006 17:34 GMT
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,
>
[quoted text clipped - 56 lines]
> >> > published list of events in the lifecyle of a winforms control? I
> >> > can't find anything on this!
Dave Sexton - 24 Oct 2006 17:47 GMT
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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.