views:

287

answers:

6

Well first of all sorry for the English. I have the following problem: I have a base form with a panel docked to the bottom and inside the panel some buttons. The forms inheriting this base form can modify the visibility of the base form buttons by properties like "ButtonCloseVisiblity" that appears in the child form properties. The problem is that sometimes when the child form refreshes (for example, when I double click an event, delete the newly created event in code and go back to the designer), the visibility of the buttons are set again to their default state. How can I solve this issue? anyone can help please.

Thanks in advance for any advice/help.

Ariel

A: 

I have used inherited forms, but not with controls on them.

I'm sorry if you have already tried this.

Try to place debug messages in various events of your base class. There may be one firing that you can to reapply your settings while still in design mode.

Nanook
+1  A: 

This is most likely down to a missing attribute on the base property. Take a look at this MSDN page, in particular DesignerSerializationVisibilityAttribute.

Chris S
A: 

Yes its happen very frequently in Visual Studio. Keep your changes in child form constructor or do them in OnLoad(). You may also add following check in case you don't want the changes to be visible in child form in design mode.

   if(!DesignMode){
    ....
   }
affan
A: 

Not sure I if completely understand, but it seems to med that the value if the visibility property is overwritten? If you change this manually in the designer file, the value will be overwritten the next time the designer file is refreshed (e.g. when you add a new control).

Try setting the value from the properties tab instead of writing it manually in the designer file.

erikric
I change the properties in the properties tab, but it gets overwritten all the time
Argons
A: 

Inheritance for visual elements on WinForms/controls in VS is not that great or that reliable - as I have found to my cost! Disappearing or rearranged controls, or overwritten properties are, I am afraid, the norm.

When I researched the issue, MS's response seemed to be that it was too hard to implement properly and that they had no plans to fix it! I seem to me that a lot of it relies upon code generation within the child control rather than true inheritance.

BTW, DesignMode is OK for a control directly on a form, but NOT reliable if you have control-on-control-on-form or any deeper hierarchy. I made either a post or an answer about this on here at some point last year.

kpollock
A: 

Digging for an answer I come to this, when I have a base form and use properties that get/set a property from a control directly like:

public bool ControlVisibility
{
    get{ return control.Visibility; }
    set{ control.Visibility = value; }
}

When modifying in the child forms, this modification is not persisted to code, and the visibility is defined by the base form, hence every time a child form is opened or the project is rebuilt the visibility of the inherited controls defaults to the base form value. In order to solve this issue we must add a field and change its value in the property:

private bool _controlVisibility;

public bool ControlVisibility
{
    get{ return _controlVisibility; }
    set
    { 
        _controlVisibility = value;
        control.Visibility = value;
    }
}

In this way the property is persisted in code.

Argons