-1

I have a form containing a flowlayoutpanel, and a user control A is added to the panel. In the constructor of user control A, a pointer to the same flowlayoutpanel is passed, so that user control A creates another user control B in the same flowlayoutpanel. The problem is that user control B is first added, then A.

Form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void addBtn_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Controls.Add(new Graphic1(this.flowLayoutPanel1));


    }
}

Graphic1.cs

public partial class Graphic1 : UserControl
{
    public Graphic1(FlowLayoutPanel flowPointer)
    {
        InitializeComponent();

        flowPointer.Controls.Add(new Graphic2());
    }
}

Graphic2.cs is just a label

problem is that Graphic2.cs is added before Graphic1.cs in the panel

user2029200
  • 93
  • 1
  • 1
  • 10

1 Answers1

0

I see three possible solutions:

  • If the constructor of control A receives the flow layout panel, have control A add itself to the flow layout panel, then add its additional controls.
  • Instead of adding the additional control in the constructor of control A, add a method such as InitializeLayout to control A. You can invoke that method after adding control A to the flow layout panel, and that method could then add any additional controls to the flow layout panel.
  • Capture any changes to the Parent property of control A (not sure whether that is possible; that depends on the GUI toolkit you are using) and add the additional controls when the parent of control A has changed.
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114