title: Keep a Control vertically and horizontally at center of its container
body: <p>I've attempted to create a custom Panel with a border around it, whose Color can be changed in order to "highlight" the Panel under certain conditions. </p>
  
  <p>The Panel will also need to communicate certain information via text. For this purpose, I've added a Label to the Panel. I've tried the prescribed methods for centering the Label but for some reason it always puts it to the top-left of the Panel. I can't set the Label's Dock to Fill because that covers up the custom border that's been created. So I need to make it so that the Label fits within the border.</p>
  
  <p>The Label's Anchor is set to None and its Location is</p>
  
  <pre><code>new Point((ClientSize.Width - Size.Width)/2, (ClientSize.Height - Size.Height)/2);
  </code></pre>
  
  <p>The code for the custom Panel is:</p>
  
  <pre><code>public class CustomPanel : Panel
  {
      public CustomPanel(int borderThickness, Color borderColor) : base()
      {
          SetStyle(ControlStyles.AllPaintingInWmPaint | 
                   ControlStyles.UserPaint | 
                   ControlStyles.OptimizedDoubleBuffer | 
                   ControlStyles.ResizeRedraw, true);
  
          BackColor = SystemColors.ActiveCaption;
          BorderStyle = BorderStyle.FixedSingle;
          Size = new Size(45, 45);
          Margin = new Padding(0);
          BorderThickness = borderThickness;
          BorderColor = borderColor;
      }
  
      protected override void OnPaint(PaintEventArgs e)
      {
          base.OnPaint(e);
  
          if (BorderStyle == BorderStyle.FixedSingle)
          {
              int halfThickness = BorderThickness / 2;
              using (Pen p = new Pen(BorderColor, BorderThickness))
              {
                  e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
                       halfThickness,
                       ClientSize.Width - BorderThickness, ClientSize.Height - BorderThickness));
              }
          }
      }
  
      public int BorderThickness { get; set; }
      public Color BorderColor { get; set; }
  }
  </code></pre>
  
  <p>And the Form code is:</p>
  
  <pre><code>private void NewPanelTest_Load(object sender, EventArgs e)
  {
      CustomPanel cp = new CustomPanel(3, Color.Black);
  
      // Create new Label
      Label info = new Label()
      {
          Size = new Size(30, 30),
          Text = "Info",
          Anchor = AnchorStyles.None,
          TextAlign = ContentAlignment.MiddleCenter,
          Enabled = false,
          Font = new Font("Microsoft Sans Serif", 6),
          ForeColor = Color.White,
          Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
      };
  
      cp.Controls.Add(info);
  
      this.Controls.Add(cp);
  }
  </code></pre>
  
  <p>EDIT: I've looked at similar questions asked and tried changing the Label's properties but with no results.</p>
  
  <pre><code>// Create new Label
  Label info = new Label()
  {
      // Same code as before
  
      // Different code
      Left = (this.ClientSize.Width - Size.Width) / 2,
      Top = (this.ClientSize.Height - Size.Height) / 2,
      //Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
  };
  </code></pre>
  
  <p>I've also tried changing the Panel's Padding, also with no results.</p>
  
  <pre><code>Padding = new Padding(5);
  </code></pre>
  
  <p>EDIT: Attempt at programatically placing Label in center of Panel (yields results of X = 0, Y = 0)</p>
  
  <pre><code>// Create new Label
  Label info = new Label()
  {
      // Same code as before (excluding "Left", "Top", and "Location")
  };
  int X = (info.ClientSize.Width - info.Width) / 2;
  int Y = (info.ClientSize.Height - info.Height) / 2;
  info.Location = new Point(X, Y);
  MessageBox.Show(info.Location.ToString());
  
  cp.Controls.Add(info);
  </code></pre>