简体   繁体   中英

Form inheritance in C#

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.

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

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.

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.

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){
    ....
   }

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 (eg when you add a new control).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM