8

I have a user control that I'm building. It's purpose is to display the status of a class to the user. Obviously, this does not matter, and will slow things down when the control runs in the IDE, as it does as soon as you add it to a form.

One way to work around this would be to have the control created and added to the controls collection of the form at run-time. But this seems less than perfect.

Is there a way to set a flag in the control so that it can skip certain sections of code based on how it is running?

p.s. I'm using C# and VS 2008

Richard C
  • 2,176
  • 5
  • 30
  • 40

3 Answers3

15
public static bool IsInRuntimeMode( IComponent component ) {
    bool ret = IsInDesignMode( component );
    return !ret;
}

public static bool IsInDesignMode( IComponent component ) {
    bool ret = false;
    if ( null != component ) {
        ISite site = component.Site;
        if ( null != site ) {
            ret = site.DesignMode;
        }
        else if ( component is System.Windows.Forms.Control ) {
            IComponent parent = ( (System.Windows.Forms.Control)component ).Parent;
            ret = IsInDesignMode( parent );
        }
    }

    return ret;
}
TcKs
  • 25,849
  • 11
  • 66
  • 104
  • Awesome! Thanks. That's a lot easier than I thougt it would be! – Richard C Dec 03 '08 at 11:58
  • Any thoughts on why Site property is always null on my Page object? – TGnat Dec 04 '08 at 16:38
  • Because webforms designer does not support this scenario. I don't know why :(. This solution works in component ( non UI components) designer and winforms designer. It should works in non-web 3rd party designers too. – TcKs Dec 04 '08 at 16:54
  • I am getting a null site object as well when used with a UserControl. Are UserControls supported? – Ben Gripka Mar 21 '13 at 21:35
  • @BenGripka Yes, UserControls are supported. However, this sample works ONLY in winforms. For ASP.NET or WPF you must use another approach. – TcKs Mar 22 '13 at 16:59
  • This approach does not work when the user control in question has already been sited to a host form and the host form is opened in the designer (giving you the ability to detect in the user control's constructor that it's in design mode). It works only if the user control is drag/dropped from the toolbox onto a design surface. If you need the former user case, use Ben's LicenseUsageMode approach instead. – Jazimov Nov 29 '21 at 13:27
6

I found this answer on another post and it seems to be easier and working better for my situation.

Detecting design mode from a Control's constructor

using System.ComponentModel;

if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
{
}
Community
  • 1
  • 1
Ben Gripka
  • 16,012
  • 6
  • 45
  • 41
0

This is the method I used in my project:

//use a Property or Field for keeping the info to avoid runtime computation
public static bool NotInDesignMode { get; } = IsNotInDesignMode();
private static bool IsNotInDesignMode()
{
    /*
    File.WriteAllLines(@"D:\1.log", new[]
    {
        LicenseManager.UsageMode.ToString(), //not always reliable, e.g. WPF app in Blend this will return RunTime
        Process.GetCurrentProcess().ProcessName, //filename without extension
        Process.GetCurrentProcess().MainModule.FileName, //full path
        Process.GetCurrentProcess().MainModule.ModuleName, //filename
        Assembly.GetEntryAssembly()?.Location, //null for WinForms app in VS IDE
        Assembly.GetEntryAssembly()?.ToString(), //null for WinForms app in VS IDE
        Assembly.GetExecutingAssembly().Location, //always return your project's output assembly info
        Assembly.GetExecutingAssembly().ToString(), //always return your project's output assembly info
    });
    //*/

    //LicenseManager.UsageMode will return RunTime if LicenseManager.context is not present.
    //So you can not return true by judging it's value is RunTime.
    if (LicenseUsageMode.Designtime == LicenseManager.UsageMode) return false;
    var procName = Process.GetCurrentProcess().ProcessName.ToLower();
    return "devenv" != procName //WinForms app in VS IDE
        && "xdesproc" != procName //WPF app in VS IDE/Blend
        && "blend" != procName //WinForms app in Blend
        //other IDE's process name if you detected by log from above
        ;
}

Attention!!!: The code returned bool is indicating NOT in design mode!

qaqz111
  • 181
  • 2
  • 7