How to tell DesignMode when not in a Designer Object

J

JPK

I am having trouble tracking down exactly where some of my general code is
being called when attempting to open the Designer in VS for one of my custom
controls. I have added " if ( DesignMode ) return; ' to all of my
disigner code, but somehow this is still being called. I wouyld like to add
if ( DesignMode ) return to this method (SomeNonUIMethod)" but DesignMode is
not usable in this non UI class. So, is there a way to tell if I am in the
Designer some other way??

Thanks,

JIM
ERROR MESSAGE VS DESIGNER::

I get this ::

Method not found: 'SomeClass SomeNonUIMethod()'.

Instances of this error (1)

1. Hide Call Stack

at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object
component, Object value)
at System.ComponentModel.Design.InheritedPropertyDescriptor.SetValue(Object
component, Object value)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager
manager, CodeAssignStatement statement, CodePropertyReferenceExpression
propertyReferenceEx, Boolean reportError)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager
manager, CodeAssignStatement statement)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager
manager, CodeStatement statement)
 
T

Techno_Dex

Use this functionality in a BaseClass (or all your classes depending on your
architecture)

Call this method from somewhere and check the results then call your
SomeNonUIMethod()
IsControlDesignerHosted(this);

/// <summary>
/// The DesignMode property is only "true" when the control that is checking
it is the control being designed.
/// If the control is on a form or another control, then DesignMode will
return false. This property will
/// walk the chain of controls from our control up the parents, ensuring
that none of the controls are in design mode.
/// This property is to serve as an enhancement to the IsDesignMode property
which contains limited functionality.
/// </summary>
private bool IsControlDesignerHosted(Control acControl)
{
if (acControl != null)
{
if (acControl.Site != null)
{
if (acControl.Site.DesignMode == true)
{
return true;
}
else
{
if (IsControlDesignerHosted(acControl.Parent) == true)
{
return true;
}
else
{
return false;
}
}
}
else
{
if (IsControlDesignerHosted(acControl.Parent) == true)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
 
J

JPK

Well,

I guess it won't work for me becuase I am not getting the error in a
Control, it is in a NON control class. I cannot find where it is being
called from or I would simply block it on the calling control with your
example code.


So, I think the solution is this instead

public bool isInDesignMode
{
get
{
return
(System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
}
}


JIM
 
T

Techno_Dex

Missed that part.... Try writing out the stack trace to the Debug window or
a log file to determine what is calling the code. Just raise and Exception,
catch it and dump the stack trace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top