Click to See Complete Forum and Search --> : Animated User Controls in Design Mode
rliq
February 3rd, 2010, 05:19 PM
I drag my Windows Forms User Control onto my form in design mode.
One of the properties of the User Control is, "Boolean Animate = false;".
This property can be changed in design mode using the Properties form within Visual Studio as it has a [Desciption("..."), Category("...")] attribute.
If it is set to true, then some text spins around (for example) and takes say 5 seconds to complete it's animation.
In design mode this is a pain (imagine having 10 of these User Controls on your form). In design mode, I just want some visual indication that the text will spin. At runtime, it should do the actual spin.
Ok, I can do all of that, but I just wanted to confirm one thing with you guys here.
Do you use...
if (DesignMode)
{
// simple visual indication
}
else
{
SpinTheText();
}
to prevent this or is there an alternative technique?
It just seems to me that the design only happens once (ok maybe a few times), whereas the application will be run many times and the "if (DesignMode)" becomes an overhead at runtime.
I understand that there will be Timer's involved in the animation and one option is to not start the Timer in design mode, but the point of this post is to determine whether "if (DesignMode)" is the way to go.
Arjay
February 3rd, 2010, 05:35 PM
Just check for design mode once while initializing the control. If not in design mode, then start the timer.
If you only check it once during initialization, you won't have a perf hit when running.
rliq
February 3rd, 2010, 05:42 PM
And by "During Initialisation", do you mean in the Constructor or the Load event?
I'm just looking for reasons/pros/cons of where to do what etc.
BigEd781
February 3rd, 2010, 05:47 PM
Just check for design mode once while initializing the control. If not in design mode, then start the timer.
If you only check it once during initialization, you won't have a perf hit when running.
I'm not sure about later versions of .NET, sor correct me if I am wrong, but in .NET 2.0 the DesignMode property is broken. I remember having to implement it myself for one of my custom controls.
rliq
February 3rd, 2010, 05:58 PM
Also, the Animate property could be set at 'runtime', so in it's set {} I will also need to check for DesignMode (runtime overhead)
Obviously I'm aiming to only have the Timer running when Animation is actually happening (even at runtime).
I'm thinking maybe when the Timer is fired. In that event maybe the only place I need to check DesignMode. Eg of the top of my head...
if (DesignMode && timer.Enabled) // runtime overhead
timer.Stop();
However, I'm still 100% that the Timer should not be started in the first place if we are in design mode.
I'm sure there is ideal logic for this kind of thing.
Arjay
February 3rd, 2010, 06:09 PM
I'm not sure about later versions of .NET, sor correct me if I am wrong, but in .NET 2.0 the DesignMode property is broken. I remember having to implement it myself for one of my custom controls.You could be right. What did you do to implement it yourself?
Arjay
February 3rd, 2010, 06:13 PM
Also, the Animate property could be set at 'runtime', so in it's set {} I will also need to check for DesignMode (runtime overhead)
Obviously I'm aiming to only have the Timer running when Animation is actually happening (even at runtime).
I'm thinking maybe when the Timer is fired. In that event maybe the only place I need to check DesignMode. Eg of the top of my head...
if (DesignMode && timer.Enabled) // runtime overhead
timer.Stop();
However, I'm still 100% that the Timer should not be started in the first place if we are in design mode.
I'm sure there is ideal logic for this kind of thing.I would go with only checking for design mode once. So do the check somewhere (probably in OnLoad) and not fire the timer rather than checking on each timer event.
You need to do this check where Design time can be checked and the constructor may not be the proper place. Maybe Ed can share some experience here?
BigEd781
February 3rd, 2010, 06:13 PM
You could be right. What did you do to implement it yourself?
hehe... trying to remember now...
Ok, found my old project. You can do it using another .NET class, just not Component.DesignMode
using System.ComponentModel;
static class EnvironmentState
{
/// <summary>
/// This is a working implementation of the Component.DesignMode property.
/// To avoid a .NET 2.0 bug (http://www.csharp-architect.com/designmodewinformsissue.aspx)
/// use this method instead. The 'bug' (they don't seem to want to fix it, so maybe it
/// is by design) causes any class derived from UserControl to return "false" regardless
/// of the current mode.
/// </summary>
public static bool DesignMode
{
get
{
return ( LicenseManager.UsageMode == LicenseUsageMode.Designtime );
}
}
}
And yes, as Arjay mentioned I simply checked for (my implementation of) DesignMode once where it was needed. In your case that would be before you start the timer.
rliq
February 3rd, 2010, 09:07 PM
So at the end of the day "DesignMode" is the correct way to go about it. Seems reasonable, as a User Control needs to work at both design time and run time. Thanks for your input.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.