C # User control with timer starts even in development mode

I have the following problem:

I created a user management library (DLL) that only has a timer enabled. When I try to use this control in the application, as soon as I drag it into the form in development mode, it starts to count! Even if the application does not work ... How can I avoid this?

My goal is that the timer starts counting down as soon as the application starts, but not in development mode ... because the timer uses some external functions that cause a crash in development mode.

Thank you for the advanced!

Dario

+4
source share
6
Timer_Tick(object sender, EventArgs e)
{
    if(this.DesignMode)
       return;
    //Rest of your code here
}
+2

, Timer - .

+1

DesignMode Control:

if (!this.DesignMode) {
    // its not in design mode.. so do stuff.
}

, , :

yourTimer.Enabled = !this.DesignMode;
+1

, , . , . , .

+1

user control form, user control Form1.Designer.cs

using System.Windows.Forms;

public class MyTimer : Timer
{
    public MyTimer()
    {
        // Set your custom timer properties here.
        this.Enabled = false;
    }
}
+1

Like many people, you can check if the control is in development mode.

The problem you may encounter is that the property Control.DesignMode does not work properly for nested controls ; he 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;
}
0
source

All Articles