DesignMode help?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

It seems to me there is no reliable way to detect design mode early enough to
prevent, running code a significant amount of code in classes developers are
writing. Currently the DesignMode flag is not set till the ISite is set
which unfortuantely happens after construction of whatever object, control or
component you are in, which means a signficant amount of code may run before
being able to determine design mode.

So how do we stop code from running if we can't detect design mode. What we
have tried to do in some cases is protected code with null checks and other
checks to avoid issues but that could make it harder to track real issues at
run time and is not always workable.

Is there a way we can determine design mode in the construction of the
object? Without that we're pretty much stuck.

Thanks
 
Try this...
private bool InDesignMode()
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
return true;
}
return false;
}
 
I tried this on a Form and I'm not sure the form's code ever get's called.
For a user control or a component this code works great and is the best
suggestion I've seen to date!

Thanks for that! Any ideas about the form thing?

Dave
 
It seems form code doesn't run at all from what I can tell at design time. I
put a message box in the constructor and it doesn't get called, tried
breakpoints there and that doesn't get called either.

I think Ideally Microsoft should have included a property that could have
been read from each class at construction time to avoid having to get license
managers and stuff. Maybe require a specific constructor for design mode for
example would have been fine.

Again your answer is the best I've seen barring Microsoft implementing the
above. \

The other responses I had seen were something along the lines of:
1. Dude don't run your code there, do it later
2. Do whatever you need to do when Site is set (not appropriate for runtime
though)
3. Check to see if your process is "devenv.exe" then you know your in
designer (yuck!)
4. Why are you using the designer?

(Ok, no one suggested the last one... but I was starting to wonder myself
when trying to find a suitable way to handle this.)

Thanks,
Dave
 
I tested this last night and it did work in the constructor with components
and controls in a test app. Just tried in our real controls here at work and
the usage mode is coming back at runtime even in the design mode at some
points.

We discovered that you can't use that code all of the time. It works great
in the constructor of the control or component, but if you try it later or in
other code it is not guaranteed to return the right value. So what we are
doing is we'll set our own design mode flag in the constructor based on this
flag.

Thanks,
Dave
 
Back
Top