After testing most of the answers here, unfortunately, nothing worked for me (VS2015). So I added a little twist to JohnV's answer, which didn't work out of the box, since DesignMode is a protected property in the Control class.
First, I made an extension method that returns the value of the DesignMode property through Reflection:
public static Boolean GetDesignMode(this Control control) { BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; PropertyInfo prop = control.GetType().GetProperty("DesignMode", bindFlags); return (Boolean)prop.GetValue(control, null); }
and then I made a function like JohnV:
public bool HostedDesignMode { get { Control parent = Parent; while (parent != null) { if (parent.GetDesignMode()) return true; parent = parent.Parent; } return DesignMode; } }
This is the only method that worked for me, avoiding all the mess of ProcessName, and while reflection should not be used lightly, in this case it all mattered !;)
EDIT:
You can also make the second function an extension method as follows:
public static Boolean IsInDesignMode(this Control control) { Control parent = control.Parent; while (parent != null) { if (parent.GetDesignMode()) { return true; } parent = parent.Parent; } return control.GetDesignMode(); }
Gpower2 Mar 16 '16 at 16:55 2016-03-16 16:55
source share