Reputation: 45
I have the following problem:
I made a user control library (DLL), which just has an enabled Timer. When I try to use this control in an application, as soon as I drag it to the Form in design mode, it starts to count! Even if the application is not running.... How can I avoid that?
My objective is that the Timer starts counting as soon as the application is launched, but not in Design Mode... because the Timer uses some external functions that cause the crash in the Design Mode.
Thank you in advanced!
Dario
Upvotes: 4
Views: 1670
Reputation: 5150
Probably you Start your Timer outside the GUI Thread and now it's ticking. I suggest this happens in the constructor of your Control. Change this to a separate method or post some Code to make a clear view of your Problem.
Upvotes: 1
Reputation: 109577
Like many people have said, you can check if the control is in design mode.
The problem you might face is that the Control.DesignMode
property doesn't work properly for nested controls; it always returns false
.
Here is a fixed version that you can use instead:
public static bool IsDesignMode(Control control)
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) // Initial quick check.
{
return true;
}
while (control != null)
{
System.ComponentModel.ISite site = control.Site;
if ((site != null) && (site.DesignMode))
return true;
control = control.Parent;
}
return false;
}
Upvotes: 0
Reputation: 3837
Besides adding new user control
from the form
, you can also create your user control
by coding at Form1.Designer.cs
using System.Windows.Forms;
public class MyTimer : Timer
{
public MyTimer()
{
// Set your custom timer properties here.
this.Enabled = false;
}
}
Upvotes: 1
Reputation: 73472
Timer_Tick(object sender, EventArgs e)
{
if(this.DesignMode)
return;
//Rest of your code here
}
Upvotes: 2
Reputation: 65079
You can check the DesignMode
property of Control
:
if (!this.DesignMode) {
// its not in design mode.. so do stuff.
}
Or perhaps neater:
yourTimer.Enabled = !this.DesignMode;
Upvotes: 3
Reputation:
Check if Timer
is not started in control's constructor - that is the most possible reason of such behavior.
Upvotes: 1