none
inherit from a user control, trouble in design mode

    Question

  • Hi,

     

    First, sorry if my question was previously asked, but I could not find the answer onthe forum (I am no native english speaker).

     

    My problem concern the inheritance of a custom User Control in design mode with Visual Studio 2010 professional: When I inherit from a custom UserControl, I cannot, move, resize or change anything in the internal componants in design mode.

    I am using C# and .net 4.0

     

    Let me give an example:

    Let say I have a control called "Father" that inherit from UserControl.  "Father" has a button (setted to public) on the left.

    I then create a control "Son" that inherit from "Father". i would like to move the button on the right for the control son. When I open "Son" in design mode, I cannot make anychange on the button: the properties are all greyed, there is a little arrow on the button and a lock appears when I click on it.

    So my question is: how do I create two controls with mostly the same functionnalities but with a different design (using the design mode)?

     

    Thanks in advance


    • Edited by Fouilloux Tuesday, November 08, 2011 10:39 AM
    Tuesday, November 08, 2011 10:39 AM

Answers

  • First, sorry if my question was previously asked, but I could not find the answer onthe forum (I am no native english speaker).

     

    My problem concern the inheritance of a custom User Control in design mode with Visual Studio 2010 professional: When I inherit from a custom UserControl, I cannot, move, resize or change anything in the internal componants in design mode.

    I am using C# and .net 4.0

     

    Let me give an example:

    Let say I have a control called "Father" that inherit from UserControl.  "Father" has a button (setted to public) on the left.

    I then create a control "Son" that inherit from "Father". i would like to move the button on the right for the control son. When I open "Son" in design mode, I cannot make anychange on the button: the properties are all greyed, there is a little arrow on the button and a lock appears when I click on it.

    So my question is: how do I create two controls with mostly the same functionnalities but with a different design (using the design mode)? 

    Thanks in advance

    Hi Fouilloux,

    Welcome to the MSDN Forum.

    As I reproduce it as you said, I can get it work:

    I create a UCFather and it is inherit form UserControl:

    public partial class UCFather : UserControl
    {....}
    

    And add a UCSon which is inherit from UCFather:

    public partial class UCSon : UCFather
    { ... }
    


    I have add the button tot he UCFather as you said:

     

    Then, I compile it and I can drag the button1 in the UCSon:

     Or it is not the steps you have done, please feel free to tell us.

    On the other hand, it is only in x86/AnyCPU mode, you can not set the build plantform as x64, or it will can not let you design it.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Neddy Ren Tuesday, November 15, 2011 8:19 AM
    Thursday, November 10, 2011 6:42 AM
  • Hi.

     

    Thanks a lot for your help.

     

    Actually, I did manage to make the example work. The problem appeared when I tried to inherit from one of my aleady build UserControl.

    As said in my last post, I found the explanation of my problem:

    The control I was trying to inherit contained a FlowLayoutPanel (Protected) that was filling the control. The result was that all the stuff added in this FlowLayoutPanel where then locked in the inherited control.

     

    As far as what I found on the internet, it is a known issue with the FlowLayoutPanel.

    One solution I found is to create the following class and use it instead of the FlowLayoutPanel

     

    [designer(typeof(System.Windows.Form.Design.ParentControlDesigner))]

    public class customFlowLayoutPanel : FlowLayoutPanel

    {

    }

     

    This is not completly satisfaying but it seems to work.

     

    Regards

    • Proposed as answer by Neddy Ren Thursday, November 10, 2011 7:59 AM
    • Marked as answer by Neddy Ren Tuesday, November 15, 2011 8:19 AM
    Thursday, November 10, 2011 7:36 AM

All replies

  • Examine the Access Modifers on the Controls.  

    By default they are set to Private, so that they cannot be changed by anything outside the class (including inherited classes); this is the behaviour that you are seeing.

    If you change the modifiers to Protected then inherited classes can change them.

     


    Regards, Phill W.
    Tuesday, November 08, 2011 12:34 PM
  • Thanks for the reply, but I already putted my controls to public.

     

    I have:

    public class Father:UserControl

    {

    public Button button1;

    ...

    }

     

    and public class Son:Father

    {

    ...

    }

    However, when I do open Father.Designer.cs, I can see an attribute "private Icontainer components". Is it that what I need to modify? If so, how because I guess modifying directly Father.Designer.cs seems a bad idea...

     

    I can also see that "Son" does not have a Son.designer.cs file attached to it.

     

    regards

     

    Tuesday, November 08, 2011 12:50 PM
  • Ok, after some other test, I m realising my problem might be harder to reproduce than what I thought.

    I can make it work if I recreate everything for the beginning. But, if I take a control that I already created, put all its inner controls to protected, then inherits from it, for some reason it does not work.

    The problem come from the fact that the "InitializeComponent" method is almost empty (normally I should contains the same thing as the "Father" one.

     

    Correction: if I set eveything (function, properties, attributes, event, delegates) to "Protected", the problem is still there: when I inherit from the control, all the inner components are seen as private by the designer
    • Edited by Fouilloux Wednesday, November 09, 2011 9:01 AM
    Tuesday, November 08, 2011 4:16 PM
  • Ok, I finally identified the problem, even thought I do not have the solution:

    In order to present my control nicely, I put all of them in a "FlowLayoutPanel".

    And guess what? for some reason I still can not explain, as soon as you're controls are in a FlowLayoutPanel, even if the "flowLayoutPanel" is Protected, the designer will not let you access them in your inherit class.

     

    Thank in advance for your reply.

    Wednesday, November 09, 2011 9:33 AM
  • First, sorry if my question was previously asked, but I could not find the answer onthe forum (I am no native english speaker).

     

    My problem concern the inheritance of a custom User Control in design mode with Visual Studio 2010 professional: When I inherit from a custom UserControl, I cannot, move, resize or change anything in the internal componants in design mode.

    I am using C# and .net 4.0

     

    Let me give an example:

    Let say I have a control called "Father" that inherit from UserControl.  "Father" has a button (setted to public) on the left.

    I then create a control "Son" that inherit from "Father". i would like to move the button on the right for the control son. When I open "Son" in design mode, I cannot make anychange on the button: the properties are all greyed, there is a little arrow on the button and a lock appears when I click on it.

    So my question is: how do I create two controls with mostly the same functionnalities but with a different design (using the design mode)? 

    Thanks in advance

    Hi Fouilloux,

    Welcome to the MSDN Forum.

    As I reproduce it as you said, I can get it work:

    I create a UCFather and it is inherit form UserControl:

    public partial class UCFather : UserControl
    {....}
    

    And add a UCSon which is inherit from UCFather:

    public partial class UCSon : UCFather
    { ... }
    


    I have add the button tot he UCFather as you said:

     

    Then, I compile it and I can drag the button1 in the UCSon:

     Or it is not the steps you have done, please feel free to tell us.

    On the other hand, it is only in x86/AnyCPU mode, you can not set the build plantform as x64, or it will can not let you design it.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Neddy Ren Tuesday, November 15, 2011 8:19 AM
    Thursday, November 10, 2011 6:42 AM
  • Hi.

     

    Thanks a lot for your help.

     

    Actually, I did manage to make the example work. The problem appeared when I tried to inherit from one of my aleady build UserControl.

    As said in my last post, I found the explanation of my problem:

    The control I was trying to inherit contained a FlowLayoutPanel (Protected) that was filling the control. The result was that all the stuff added in this FlowLayoutPanel where then locked in the inherited control.

     

    As far as what I found on the internet, it is a known issue with the FlowLayoutPanel.

    One solution I found is to create the following class and use it instead of the FlowLayoutPanel

     

    [designer(typeof(System.Windows.Form.Design.ParentControlDesigner))]

    public class customFlowLayoutPanel : FlowLayoutPanel

    {

    }

     

    This is not completly satisfaying but it seems to work.

     

    Regards

    • Proposed as answer by Neddy Ren Thursday, November 10, 2011 7:59 AM
    • Marked as answer by Neddy Ren Tuesday, November 15, 2011 8:19 AM
    Thursday, November 10, 2011 7:36 AM