none
Question Inherited Form with Control

    Question

  • Hello I have created an inherited form.
    There are several controls as StatusStrip, GridView and ToolStrip

    In the inherited form I want to add a ProgressBar on the StatusStrip (in the Design View). But all the controls are locked.

    How can I change the controls in the Inherited Form?

    Can somebody help me?


    Thank you. Wes.
    Friday, February 12, 2010 4:30 PM

Answers

  • Change the access modifier in the parent form for those components to protected I believe. (you do this in the designer properties)
    Friday, February 12, 2010 5:08 PM
  • The designer cannot change controls from the parent form.
    You can call any method or property of the public or protected members of your base class in your code.

    Change the Modifiers property of the statusstrip to public or protected.
    And write the code to add the progressbar to the statusstrip in the constructor of the derived form.

    Friday, February 12, 2010 5:40 PM

All replies

  • Change the access modifier in the parent form for those components to protected I believe. (you do this in the designer properties)
    Friday, February 12, 2010 5:08 PM
  • Hello I have created an inherited form.
    There are several controls as StatusStrip, GridView and ToolStrip

    In the inherited form I want to add a ProgressBar on the StatusStrip (in the Design View). But all the controls are locked.

    How can I change the controls in the Inherited Form?

    Can somebody help me?


    Thank you. Wes.


    For questions related to WinForms and any WinForm control, including DataGridView use the Windows Forms General or Windows Forms Data Controls and Databinding forums.  But initially, this is more of language issue that a Forms issue.

    You cannot change the base class members/controls in a derived class.  That is whole point of inheritance.  The derived class "is-a" base class object.  If the derived classes could modify the base class, then they would no longer be inheriting from the original base class.  They would inheriting from something similar but yet different.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 5:12 PM
    Moderator
  • Change the access modifier in the parent form for those components to protected I believe. (you do this in the designer properties)



    I need to try that and see if it works, but I don't expect it to. 
    If one could do that, it would amount to modifying the base class. 
    So let's suppose that you could make changes to the base class members/controls. 

    What is supposed to happen with other types that inherit the same base class? 
    Shouldn't their base classes be modified, too?

    Besides, the base class that you see in the Form Designer could be coming from an external assembly.
    It would be nearly impossible to modify the base class source code under that scenario.
    The base class source code would need to be re-compiled, and re-referenced.
    And doing so would break the source code for other types that use the type.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 5:16 PM
    Moderator
  • The designer cannot change controls from the parent form.
    You can call any method or property of the public or protected members of your base class in your code.

    Change the Modifiers property of the statusstrip to public or protected.
    And write the code to add the progressbar to the statusstrip in the constructor of the derived form.

    Friday, February 12, 2010 5:40 PM
  • I need to try that and see if it works, but I don't expect it to. 
    If one could do that, it would amount to modifying the base class. 
    So let's suppose that you could make changes to the base class members/controls. 

    What is supposed to happen with other types that inherit the same base class? 
    Shouldn't their base classes be modified, too?

    Besides, the base class that you see in the Form Designer could be coming from an external assembly.
    It would be nearly impossible to modify the base class source code under that scenario.
    The base class source code would need to be re-compiled, and re-referenced.
    And doing so would break the source code for other types that use the type.

    Who is talking about changing the base class?
    Friday, February 12, 2010 5:43 PM
  • Hello I have created an inherited form.
    There are several controls as StatusStrip, GridView and ToolStrip

    In the inherited form I want to add a ProgressBar on the StatusStrip (in the Design View).

    But all the controls are locked.

    How can I change the controls in the Inherited Form?



    Louis.

    Right there is where the OP is trying to modify the base class controls. 
    The Form Designer displays lock icons on the base class controls.
    It sounds to me like the base class controls are in the way because the base class form is already fairly full.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 5:51 PM
    Moderator
  • Adding a ProgressBar to a StatusStrip that has been declared in the base Form is just a method call.
    With the Modifier property set to 'public' or 'protected', the statusStrip is inherited and I can use it as I want:

    this.statusStrip1.Items.Add(myProgressBar);

    It's nothing more than what is being done all the time when you add a control on a form:

    this.Controls.Add(myControl);
    Friday, February 12, 2010 6:03 PM
  • Adding a ProgressBar to a StatusStrip that has been declared in the base Form is just a method call.
    With the Modifier property set to 'public' or 'protected', the statusStrip is inherited and I can use it as I want:

    this.statusStrip1.Items.Add(myProgressBar);

    It's nothing more than what is being done all the time when you add a control on a form:

    this.Controls.Add(myControl);


    I agree with you.
     
    But, I had thought the issue was that base class controls are in the way of adding new controls in the inherited class.
    The Form Designer locks all of the base class controls for the reasons I cited.

    Any new controls added by inheriting classes must be dropped around the base class controls.
    The base class control positions are locked in place by the Form Designer.  Try it.

    Rudy

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 6:11 PM
    Moderator
  • The designer can change many controls inherited from the base form (Form / UserControl) if they are accessible to the derived form.
    As Alan and Louis mentioned, access modifier can be set in the base form design view through the Modifiers property for each control on the form.

    AFAIK the control designer can override this behavior through InheritanceAttribute which indicates whether the associated control has been inherited from the base form.
    For some controls, and unfortunately for you this is true for ToolStrip, StatusStrip and DataGridView, the designer (associated with the control - ToolStripDesigner, DataGridViewDesigner) will lock the inherited control regardless of it's accessibility on the derived form, overriding default behavior:

    protected override InheritanceAttribute InheritanceAttribute
    {
        get
        {
            if (base.InheritanceAttribute == InheritanceAttribute.Inherited)
            {
                return InheritanceAttribute.InheritedReadOnly;
            }
            return base.InheritanceAttribute;
        }
    }
    

    Bummer, I guess they had a good reason for locking some inherited controls but that's really beyond me ... you best use the approach Louis described.

    BTW, changing inherited controls in the design view does not change the base class and has no effect on it, so it does not break inheritance concept in any way.

    EDIT: You can try to add button, panel, textbox .. on the base form and set Modifiers property to protected. You will be able to move and change them in every way in the derived form, including adding new controls in the inherited panel.

    Best regards, 
    Vladimir


    • Edited by Vladimir.Ilic Friday, February 12, 2010 6:50 PM clarify which designer locks the control
    Friday, February 12, 2010 6:32 PM
  • The Form Designer locks all of the base class controls for the reasons I cited.
    Seems that it doesn't lock container controls such as Panel which i tried.

    I think designer behavior is not logical.
    For example I add a public DataGridView to base form then it locked in derived form in design-mode. I couldn't add any column to that DataGidView in derived form designer however I could add a sample column to it programmaticaly.

    I don't know what cause this limitation in VS.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 6:36 PM
  • It makes sense, and believe it or not Louis, Alan and I have been saying the same thing from the opposite sides of the fence. 

    They have suggested that the OP needs to modify the base class. 
    Change the access modifiers on the controls from "private" to "protected".
    Then you must re-compile the base class, and now the inherited class can modify the "protected" control.

    I have been saying that if the Designer could modify these "locked" controls, it would have to modify the base class.
    Make the exact modifications that the others have suggested, and then re-compile, on the fly.
    That would be impossible for the Designer to do.  Behavior must be constant under all scenarios.
    If the type came from a library, for which you did not own the source code, such a change would be impossible.

    Rudy  =8^D

    Keep up the good contributions Yasser.
    Your intellectual debates are appreciated.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 6:51 PM
    Moderator
  • Thanks, Rudy
    I found an answer @ Cannot modify DataGridView properties in inherited control

    But it is old(2005), i don't know if DataGridView supports it in VS2010.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 7:21 PM
  • Nice link. 
    We're back to having to modify the code in the base class again, though.
    Does the behavior make sense to you now?

    I'm pretty sure it will work the same way for every future release of Visual Studio.
    No more changes for that namespace on the horizon. 
    They broke up the WinForms design team nearly two years ago.
    No changes in the DLL since .NET 2.0.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 7:30 PM
    Moderator
  • So maybe WPF design team addresses this feature.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 7:45 PM
  • I think this is a basic programming issue, not a UI Designer issue.

    Think about it.  When you create a custom control, the Form Designer does not see it in the Toolbox until you compile the code.  Now you can go out and grab that control and drop it on a form's surface using the Designer. 

    Let's say taht you make certain types of changes to the source code of the control, and re-compile the assembly.  Now you go to open the Form in the Designer and you discover that it doesn't open.  The form has crashed the designer.  There have been countless posts about designer crashes, too.

    For lack of a better term, the Designer uses a "static" definition of the control.  It uses the definition from the compiled assembly that the defined the control when you picked it from the Toolbox.  If that assembly changes, the Designer can crash.  The Designer sees the properties and class members and writes the code into the designer.cs file. 

    For example, suppose you delete or rename properties that were used by the Designer in the InitializeComponents method.  When the Designer goes to open the form for Design Mode, the code doesn't compile because the property name no longer exists in the type definition.  Result, the Designer cannot instantiate the form.

    I see how a similar problem exists for inherited forms, except from the opposite direction.  In order to modify the base class controls, the base class needs to be modified in some way.  This means that the base class would need to be re-compiled, and the resulting assembly may need to be re-referenced.  The Form Designer would need to do this all inside of a button click as you try to re-position a base class control.  That is not going to happen.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 7:58 PM
    Moderator
  • Not exactly...
    For example I puted a Panel from tool-box to base form, then I set it's access-modifier to protected, finally i put some different controls such as a DataGridView, TextBox,... with private access-modifier to that Panel.
    In this situation I could re-position the panel with all of it's contents in derived form designer without re-compilation.
    The DataGridView is private but I could access it throuth base.panel.Controls[...

    I think some behavior like above are not logical in object-oriented basics, but they can have some logical design-reason for this limitation, though.


    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 8:31 PM
  • That's because you declared the Panel's accessibility as "protected". 
    Change it to "private" on a test form.  Access denied.  Designer shows that lock icon it.

    This the problem that the OP faces. 
    Those little lock icons on the controls.
    The OP cannot modify the inherited controls.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 8:47 PM
    Moderator
  • Designer supports re-positioning an inherited Panel or Label but it doesn't support this for an inherited DataGridView? (with protected access-modifier)
    In your opinion, what is the logic behind this behavior?


    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 8:59 PM
  • Ok Rudy,

    the problem here is even if you have the source code of the base form (and in most cases you designed the base form within the same project / solution, and i believe that is the problem OP faces) and you go and change the access modifier for a child control to protected, some of the controls will still be locked in the derived form designer.

    I don't think that this is logical and I have to presume that there is a strong reason why Win Forms development team took an extra step to lock these controls on the derived form.
    As i said before they did it in the associated designers - DataGridViewDesigner and ToolstripDesigner, which are by the way declared internal so can not be subclassed.

    Friday, February 12, 2010 9:02 PM
  • Logic?  There isn't any. 

    Educated guesses.
    The Designer assumes that the DGV might be referenced in a DataSet or TableAdapter?
    I am sure that it was done for good reason, though.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 9:07 PM
    Moderator
  • Vladimir, we posted almost the same conclusion at the same time.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, February 12, 2010 9:09 PM
    Moderator
  • Yes, I'm agree. that isn't logical, however have some satisfier reason, I guess.
    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Friday, February 12, 2010 9:20 PM
  • It seems that it isn't possible to change the derived form in the Designer View modus.

    But I can add now a control as a Progressbar in StatusStrip. I have to use the ToolStripProgressBar in stead off the ProgressBar-control.
    This solution is good enough for me.

    Thanks for the reactions.
    Friday, February 12, 2010 10:55 PM