471,316 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Detecting if a UserConrtol is in designer or run-time

Hi,

I have a user control which connects to a database in it's OnLoad event
handler. When the user control is on a form and i open this form in VS
designer, i get an exception regarding database connection. What is the
way to detect that a user control is loaded in designer vs runtime?

Thank you,
MuZZy
Jan 23 '06 #1
7 10756
if (!this.DesignMode)
{
...
}

or, conversely,

if (this.DesignMode)
{
return;
}

which is what I most often use.

Note that the DesignMode property returns a sensible result only after
the component's window handle has been assigned. In other words, it
doesn't work in the constructor. OnLoad is a good place to test it.

Jan 23 '06 #2
Bruce Wood wrote:
if (!this.DesignMode)
{
...
}

or, conversely,

if (this.DesignMode)
{
return;
}

which is what I most often use.

Note that the DesignMode property returns a sensible result only after
the component's window handle has been assigned. In other words, it
doesn't work in the constructor. OnLoad is a good place to test it.


Oh my... that's why it didn't work for me -i had this check in constructor.
By the way, what if i need to test it in constructor - can i somehow?

Thank you,
MuZZy
Jan 23 '06 #3
Not that I know of. You have to move all "run-time only" initialization
code into the Load method.

Jan 23 '06 #4
Bruce Wood wrote:
Not that I know of. You have to move all "run-time only" initialization
code into the Load method.

The problem with that is that i might need to access some properties of
the user control after it's being created but before it's being loaded
and if i move all init code into OnLoad i will get in trouble as the
control might not be initialized when i access it
Jan 23 '06 #5
You don't have to move _all_ init code... just the init code that
shouldn't run at design time. Sorry, but I've never found a way around
that.

Jan 23 '06 #6
Bruce Wood wrote:
You don't have to move _all_ init code... just the init code that
shouldn't run at design time. Sorry, but I've never found a way around
that.


Thanks for your help! With some minor tweaks i've finally fixed the control.
Jan 23 '06 #7
Jon
Try

protected static bool IsInDesigner {
get { return ( Assembly.GetEntryAssembly() == null ); }
}

Feb 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael Albanese | last post: by
1 post views Thread by Raj Chudasama | last post: by
1 post views Thread by bbrunner | last post: by
1 post views Thread by Chuck Haeberle | last post: by
27 posts views Thread by Deek | last post: by
6 posts views Thread by TryingLikeHeck | last post: by
4 posts views Thread by Dave Taylor | last post: by
4 posts views Thread by Rod Gill | last post: by
3 posts views Thread by Graeme Hinchliffe | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.