Reputation: 609
I created a UserControl1 : UserControl, where I defined VisibleNew property as
[Browsable(true)]
public bool VisibleNew
{
get
{
return Visible;
}
set
{
Visible = value;
}
}
So, if the control is placed on form, setting Visible = false does not hide it from the designer. But setting VisibleNew = false hides it!
What is wrong? How to make VisibleNew not to hide the control in designe time? Tested on VS2010, VS2012
Upvotes: 0
Views: 507
Reputation: 942348
Controls have a designer, a class that runs at design time and make the control behave differently. The existing designer for a UserControl will intercept any assignments to the Visible property to prevent the control from getting invisible at design time. Problem is, it doesn't know beans about your VisibleNew property so cannot intercept it.
Technically you can create your own designer and have it intercept VisibleNew as well. It is however much simpler to build design-time awareness directly into your class. You can use the DesignMode property to detect that your control is being used at design time. Like this:
private bool visibleNew = true;
[Browsable(true), DefaultValue(true)]
public bool VisibleNew {
get {
return visibleNew;
}
set {
visibleNew = value;
if (!this.DesignMode) base.Visible = visibleNew;
}
}
Putting on my psychic debugging glasses, I don't think you actually want to do this at all. Tinkering with the Visible property is very tricky, it has quirky behavior at runtime. It will only return true if the control is actually visible to the user. I can only imagine you want to create your own property to work around that behavior. The correct way to do that looks like this:
private bool visibleIntent;
protected override void SetVisibleCore(bool value) {
visibleIntent = value;
base.SetVisibleCore(value);
}
[Browsable(false)]
public bool VisibleIntent {
get { return visibleIntent; }
}
Upvotes: 1