How to center panel within groupbox?

Asked

Viewed 170 times

0

How can I center panel1 within groupbox ?

Follows code:

private void Form1_Load(object sender, EventArgs e)
{
    // aqui defino novo size do panel1
    panel1.Size = new Size(591, 423);
}

The problem is that the panel1 is not centralized.

Some solution ?

2 answers

1

Problem solved:

panel1.Location = new Point(
    groupBox1.Width / 2 - panel1.Size.Width / 2,
    groupBox1.Height / 2 - panel1.Size.Height / 2);
panel1.Anchor = AnchorStyles.None;

0

It is also possible to align to properties .Top and .Left depending on your needs.

Alignment within a control or component is a fairly easy thing. Come on!

Knowing that the center of the container is half its width .Width / 2 and in half its height .Heigth / 2

control. Top = (Container.Heigth / 2) - (control.Heigth - 2);

control. Left = (Container.Width / 2) - (control.width - 2);

Note: the container is nothing more, than its container.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.