0

I need to add multiple panel into main panel, when I added second panel it will move on top the first added panel. please refer the image if you can't get what i means.

the question is, how can I add the following panel below the panel I just add?

public override DockStyle Dock { get; set; }
private void resultlabel()
    {

       Panel panel1 = new Panel();
       panel1.Height = 50;
       panel1.Dock = DockStyle.Top;
       panel1.AutoSize = false;
       panel1.AutoSizeMode = AutoSizeMode.GrowOnly;
       panel1.AllowDrop = false;
       panel1.CausesValidation = true;
       if((totalitem % 2) == 0)
        {
          panel1.BackColor = Color.Blue;
        }
        else
        {
          panel1.BackColor = Color.Orange;
        }
       Label label1 = new Label();
       label1.Text = count.ToString();
       panel1.Controls.Add(label1);
       mainPanel.Controls.Add(panel1);
       count= count+ 1;
    }

enter image description here

Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
Mizzle Lee
  • 125
  • 2
  • 7

3 Answers3

0

You get the result because of this:

panel1.Dock = DockStyle.Top;

So each panel you add is added to the top. Can you add the panels in a reverse order? That would be a quick solution, otherwise you could look into TableLayoutPanel or FlowLayoutPanel if that would be a useful approach for your problem.

Bojan B
  • 2,091
  • 4
  • 18
  • 26
0

You need to change your z-order to change the order of docked panels. Your best bet not to change a lot of code would be using:

panel1.BringToFront();

Or

panel1.SendToBack();

Depending on the order you want

This needs to be done after adding it to mainPanel (that is, after the mainPanel.Controls.Add(panel1); line)

Jcl
  • 27,696
  • 5
  • 61
  • 92
0

Try to use panel1.Dock = DockStyle.Bottom; intead of : panel1.Dock = DockStyle.Top;

Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17