How to center a shortcut inside a panel without installing Dock to Fill

I tried to create a custom panel with a frame around it, the color of which can be changed to "highlight" the panel under certain conditions.

The group will also need to convey certain information through text. For this purpose, I added a shortcut to the panel. I tried the prescribed methods for centering the label, but for some reason, it always places it in the upper left of the panel. I cannot set the Bright Dock to fill because it closes the created user border. Therefore, I need to make the label fit in the frame.

The anchor shortcut is set to None and its location

new Point((ClientSize.Width - Size.Width)/2, (ClientSize.Height - Size.Height)/2);

Code for custom panel:

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; }
}

And the form 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);
}

EDIT: Label, .

// 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)
};

Padding, - .

Padding = new Padding(5);

: ( X = 0, Y = 0)

// 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);
+4
4

enter image description here

  • Label AutoSize false;
  • Label TextAlign MiddleCenter;

.

   int x = (panel1.Size.Width - label1.Size.Width) / 2;
    label1.Location = new Point(x, label1.Location.Y);
+6

- TableLayoutPanel 1 1 Panel. Label, , Anchor None, .

2 1

CellPaint TableLayoutPanel :

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    var r = e.CellBounds;
    r.Width--;
    r.Height--;
    e.Graphics.DrawRectangle(Pens.Red, r);
}
+1

, . , , , , , , , , . Visual # Windows Forms. , . :

  • ,
  • TextAlign MiddleCenter

,

        public void Centroid(Label label, Panel parent)
        {
            int x = (parent.Size.Width - label.Size.Width) / 2;
            label.Location = new Point(x, label.Location.Y);
        }

: Centroid (label1, panel1); , label1 & panel 1. , .

, :)

0
source

I did the horizontal alignment of the center of the panel and label as follows:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        label1 = new System.Windows.Forms.Label();
        this.SuspendLayout(); 
        this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 23F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1401, 462);
        this.Controls.Add(this.label1);
        this.Font = new System.Drawing.Font("Times New Roman", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Margin = new System.Windows.Forms.Padding(6, 5, 6, 5);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();


        int borderThickness = 5;
        Color borderColor = Color.Cyan;
        CustomPanel panel1 = new CustomPanel(borderThickness, borderColor);
        panel1.BackColor = Color.Yellow;
        panel1.Location = new Point(400, 30);
        panel1.Size = new Size(300, 300);
        panel1.Parent = this;
        this.Controls.Add(panel1);

        label1.Name = "label1";
        label1.TabIndex = 0;
        label1.AutoSize = true;
        label1.ForeColor = Color.Black;
        label1.Text = "this is the text whose center I want to align";
        label1.Location = new Point(panel1.Location.X + panel1.Width / 2 - label1.Width / 2, 80);
        if (this.Controls.Contains(label1))
        {
            label1.BringToFront();
        }
    }

    private Label label1;
}

Since I posted the answer, I found that there is the following statement to align the label in the center of the panel:

this.Controls.Add(label1);

MUST be located after application:

label1 = new Label();

and before approval:

label1.Location = new Point(panel1.Location.X + panel1.Width / 2 - label1.Width / 2, 80);
0
source

Source: https://habr.com/ru/post/1651420/


All Articles