Flowlayoutpanel control in a child form can not be modified

flavous lei 21 Reputation points
2020-06-23T05:10:29.6+00:00

when the father form has a flowlayoutpanel control, child form can not modify its any attribute though the modifiers of flowlayoutpanel is public. But other controls of father form like button can be modified in child form.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-06-23T07:59:14.353+00:00

    I test the method that provide in the SO thread, and it works fine. Here is a workaround you can refer to.
    We can replace the FlowLayoutPanel with a custom FlowLayoutPanel class.

    Parent.Designer.cs:

      partial class Form1
    {
        // Code omitted
        // …
            private void InitializeComponent()
            {
                flowLayoutPanel1 = new FlowLayoutPanelHeritable();
                this.SuspendLayout();
                // 
                // flowLayoutPanel1
                // 
                flowLayoutPanel1.Location = new System.Drawing.Point(24, 12);
                flowLayoutPanel1.Name = "flowLayoutPanel1";
                flowLayoutPanel1.Size = new System.Drawing.Size(285, 174);
                flowLayoutPanel1.TabIndex = 0;
                // Code omitted
                // …
            }
    
            // declare a custom FlowLayoutPanel
            public FlowLayoutPanelHeritable flowLayoutPanel1;
            public System.Windows.Forms.Button button1;
        }
    
        // Custom FlowLayoutPanel inherited from FlowLaypitPanel
        [Designer(typeof(ParentControlDesigner))]
        public class FlowLayoutPanelHeritable : FlowLayoutPanel
        {
        }
    

    Hope this can help you.


1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-06-23T07:06:16.207+00:00

    What is the “child form” you mentioned? A form opened from “father form” by clicking a button? Or an MDI child form?
    If is the former, I tested it and it works fine. The following is the test demo.

    Parent Form:

      public FlowLayoutPanel FLP  
        {  
            get { return flowLayoutPanel1; }  
            set { flowLayoutPanel1 = value; }  
        }  
      
        private void btOpenChild_Click(object sender, EventArgs e)  
        {  
            Form2 form = new Form2();  
            form.Show();  
    }  
      
    

    Child Form:

     private void btModifyPanel_Click(object sender, EventArgs e)  
        {  
            Form1 form1 = (Form1)Application.OpenForms["Form1"];  
            form1.FLP.BackColor = Color.Red;  
    }  
    

    10458-demo.gif
    If is an MDI child form, please provide some related code to reproduce the issue.