0

I'm using WinForms. In my form i have 2 panels which i want to dock up or down on button click.

The issue i'm running into is that my panels is not docking correctly. When i click on the up button, panel one label gets covered by panel2.

Panel 1: (Anchor: Top, Left, Right)

Panel 2: (Anchor: Top, Bottom, Left, Right)

    private void Up_Btn_Click(object sender, EventArgs e)
    {
        panel1.Dock = System.Windows.Forms.DockStyle.Fill;

        panel2.Dock = System.Windows.Forms.DockStyle.Top;
    }

    private void Down_Btn_Click(object sender, EventArgs e)
    {
        panel1.Dock = System.Windows.Forms.DockStyle.Fill;

        panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
    }

Incorrect the label should not be covered by the panels

enter image description here

What supposed to happen when Up button is clicked

enter image description here

What supposed to happen when Down button is clicked

enter image description here

taji01
  • 2,527
  • 8
  • 36
  • 80
  • 1
    Panel1 fills the entire form, Panel2 overlaps it. Thus hiding the label. You'll have to rearrange the Z-order the controls, this.Controls.SetChildIndex() method. – Hans Passant Dec 23 '15 at 21:03

1 Answers1

0
public Form1()
{
    InitializeComponent();
    panel1.BringToFront();
}

private void Up_Click(object sender, EventArgs e)
{
    panel1.Dock = DockStyle.Fill;
    panel2.Dock = DockStyle.Top;
}

private void Down_Click(object sender, EventArgs e)
{
    panel1.Dock = DockStyle.Fill;
    panel2.Dock = DockStyle.Bottom;
}

The trick is to correct the order of the controls.

See here: Docking multiple controls - one fills remaining space

Community
  • 1
  • 1
Smudge202
  • 4,689
  • 2
  • 26
  • 44