Hi Frank,
Firstly, when we add a control/component onto a form in design time, the
form designer assigns a value to the Site property of the
control/component. At run time, the control/component Site property is
null. The DesignMode propery of the control/component depends on the Site
property, i.e. it returns true if the Site property is not null and returns
false if the Site property is null.
Secondly, if we add a UserControl into another UserControl, and then add
the composite UserControl onto a form, the form designer would only assign
a value to the Site property of the composite UserControl, which means the
Site property of the nested UserControl remains null so that the DesignMode
propery of the nested UserControl returns false. This is the reason of your
problem.
A solution is to use the value of the DesignMode property of the composite
UserControl in the nested UserControl to determine whether it is in design
time now. To do this, we could add a public property in the composite
UserControl to get its DesignMode property value.
The following is a sample. The UserControl1 below is the composite
UserControl and the UserControl2 is the nested UserControl.
public partial class UserControl1 : UserControl
{
public bool IsDesignTime
{
get { return this.DesignMode ; }
}
}
public partial class UserControl2 : UserControl
{
private bool IsDesignTime
{
get
{
if (this.DesignMod e)
return true;
else
{
Control ctrl = this;
while (ctrl.Parent != null && !(ctrl.Parent is Form))
{
ctrl = ctrl.Parent;
}
UserControl1 uc1 = ctrl as UserControl1;
System.Diagnost ics.Debug.Asser t(uc1 != null, "the
container must be UserControl1");
return uc1.IsDesignTim e;
}
}
}
}
Then, we use the IsDesignTime property in the UserControl2 instead of the
DesignMode property.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.