Answered Inherit from a base form

  • Friday, May 11, 2012 1:21 PM
     
     

    Hi all,

    I've got a base form with a button anchored to the bottom, right. I inherit this base form, and the inherited form has additional fields that I add to those belonging to the base form. However, when I resize the inherited form, the button doesn't anchor correctly and it stays at its original position as in the base form.

    Furthermore, I set the base form so that it doesn't resize. However the inherited forms can be resized.

    Any ideas?

    Thanks,

    Ivan

    • Moved by CoolDadTxMVP Friday, May 11, 2012 3:36 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Friday, May 11, 2012 3:12 PM
     
     

    Just tried this out in a sample project and the button on the inherited form anchored correctly. 

    I did the following:

    1. BaseForm - FormBorderStyle: FixedSingle - added a button with anchoring set to Bottom + Right

    2. InheritedForm - FormBorderStyle: Sizable - inherits from the BaseForm

    When showing the InheritedForm, this form could be resized and the button anchored to the bottom right as expected.

    Can you post some of your code / form designer files to see if I have understood your problem.

  • Friday, May 11, 2012 5:28 PM
     
     

    The Form Designer should not allow you to modify any of the base class form members.  You should see miniature lock icons next to those controls defined in the base class; i.e. like your button.  This behavior is by design and cannot be changed.  Think about it.  It would be a disaster if you could.  Here's why.

    Suppose you define your base class form with a Button.  You inherit this base class in FormA and FormB.  You open up FormA, add more controls, and move the button slightly.  You  open up FormB, add more controls, and realize the button needs to be moved a pinch.  You re-open FormA and realize that it is broken because the changes you made to the Button also show up in FormA, and vice versa. 

    Furthermore, in order for the Form Designer to modify the base form it would need access to the source code and a compiled version of it.  The source code may not always be available for the base class form.  Suppose the base class form came from a 3rd party library?  For these and other good reasons, you cannot modify the base class form/control in a derived class using the Form Designer.

    Hope this helps.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • Friday, May 11, 2012 6:19 PM
     
     

    I don't want to move the button on FormB but as it is anchored to Bottom+Right shouldn't it move further down in FormB when I increase the height?

    To replicate this problem, do the following:

    Create a base FormA, add a couple of textboxes and a button anchored bottom+right below them. Now create an inherited FormB, double its height and add an additional couple of textboxes. Whilst increasing FormB's height you will see that the button is moving correctly.

    The first time you do this, the button is anchored properly. But if you close FormB and open it again, the button is moved back to its original location specified in FormA.

    Is this by design or a problem?

    Ivan

  • Friday, May 11, 2012 6:45 PM
     
     

    That is by design.  Create a test derived form.  Try building it.  Open the derived form, and change the size.  Make it wider or something.  Now build it without closing the Designer. ??? It jumps back to its' original location right before your eyes!

    Inheritance based code designs usually run into problems.  Google this phrase " favor composition over inheritance ".

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • Friday, May 11, 2012 9:33 PM
     
     Answered

    "Inheritance based code designs usually run into problems.  Google this phrase " favor composition over inheritance "."

    Inheritance works well if you supply the code and avoid designer code.  After a while, no standard control does what you want and you have to inherit everything.  That means, you have to eliminate the designer and code your UI.

    • Marked As Answer by Cryo75 Saturday, May 12, 2012 11:24 AM
    •  
  • Sunday, May 13, 2012 3:01 AM
     
     
    If you create an instance of the base form, does it behave correctly?

    --
    Mike
  • Monday, May 14, 2012 11:38 AM
     
     

    In your inherited form, are you overriding OnLayout

    If so, do you call base.OnLayout, preferably before doing anything else? 


    Regards, Phill W.