Lets say you've just created a user control or customized an existing control from the .Net Framework and you want the bahavior to be different when you're in design mode vs execution mode. How can you do it?
| Solution #1: DesignMode Property
The first an very intuitive approach is to use the DesignMode property of your control. The problem is that it will always return false if it has a parent... not so great
Solution #2: Design Mode Property with a twist
The solution then is to go up the control hierarchy and find the parent with the right value!
The resulting code will look a little like this:
public bool IsDesignMode ()
{
Control instance = this;
while (instance != null)
{
if (instance.Site == null) return false;
if (instance.Site.DesignMode) return true;
instance = instance.Parent;
}
return false;
}
Using this method can prevent errors where the GUI does not draw properly in design mode because it tries to access data (i.e. database connection)
The biggest limitation is that you can't call the method from the constructor because the parent may not be set yet!
Of course there are other solutions but they are not as elegant. Here's one of them:
bool designMode = Application.CompanyName == "Microsoft Corporation";
This solution works from the constructor, so it does have its advantages...
|
bec6a4a0-fd55-4292-9cb0-2517c209aadf|0|.0