all groups > visual studio .net ide > february 2006 >
You're in the

visual studio .net ide

group:

VS.net shouldn't call custom control initializer!


VS.net shouldn't call custom control initializer! commanderjason NO[at]SPAM gmail.com
2/27/2006 10:07:40 AM
visual studio .net ide:
I have a recurring problem with the forms designer is VS.net 2003.

If you want to add a UserControl to your form, vs.net will call that
controls initialization code when placing the control in the form. I
believe this behavior only causes problems. I could care less if the
designer can initialize the control at design time as long as the
control works at runtime. I would be perfectly happy if the forms
designer just used a place holder for the control for the purpose of
placing and defining the visual behavior of the control.

The problem is that my controls require a bunch of resources to load,
one resource for example is the app.config file. I have many things in
the config file that my custom control has to load during the controls
initialization block. Internationalization settings, file paths,
perhaps db connection strings, the options are endless.

But as ive seen in the forms designer many resources such as the config
file, perhaps language and image resource files, are not available when
the control is loaded in the designer, only at runtime.

Therefore im forced to add all these try/catch blocks and generally
re-write my controls so the designer doesnt choke on something it
shouldn't need to do anyway.


Another annoyance of this behavior is that if i have my form is all
designed and setup, then i go and add some init code to the usercontrol
that uses some runtime resource such as the config file, the designer
will remove my control and basically erase the control and i have to
re-setup and size any and all controls that the designer deems unworthy
of running even though the code can and will execute flawlessly at
run-time.

Does anyone have any strategies that may prevent the designer from
running my control init code, or otherwise deal with this very annoying
problem?
Re: VS.net shouldn't call custom control initializer! George Jordanov Ivanov
2/27/2006 3:57:07 PM
Hi Jason,

You need to check the user control's DesignMode property - it is set to
true, if your control is in design-time mode, otherwise - false. Thus, the
initialize logic in your user control should be executed only if this
property is false.

Check out MSDN online for this specific property at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodelcomponentclassdesignmodetopic.asp

Regards,
George Jordanov Ivanov

[quoted text, click to view]

Re: VS.net shouldn't call custom control initializer! Jeff Pek (Autodesk)
3/26/2006 12:00:00 AM
The only problem is that this property doesn't always return the correct
value. There are many blog, etc. posts on this. Apparently this still has
not been fixed in VS2005.

Here's one of many blog posts that talks about this:
http://blogs.vbcity.com/hotdog/archive/2005/10/21/5596.aspx

It makes a couple of points about its use, and suggests some alternatives
that are more reliable.

Jeff

[quoted text, click to view]

AddThis Social Bookmark Button