Search code examples
c#alignmentcentering

How can I center a label in the middle of a panel in C#?


I am trying to make an Icon Generator Application in C#. I have to panels, one that is in charge of the background colour (Border colour) and the other as foreground colour (Fill colour). I also have a label in the middle of the forgeground colour panel (Fill colour) see screenshots designer, application ran for first time, changed code using textbox and button (see arrow in screenshot)

I have tried using tablelayout panels and docking but it doesnt seem to work. I basically want to do what the designer align buttons do but through code.

Any help is greatly appriciated thanks!


Solution

  •  Label myLabel;
     Panel myPanel;
      int x = (myPanel.Size.Width - myLabel.Size.Width) / 2;
      int y = (myPanel.Size.Height - myLabel.Size.Height)/2;
        label1.Location = new Point(x, y);