161

In WinForms I am using a Label to display different messages like success, failure, etc.

I'd like to center that label in the center form. I want a solution that will keep it centered whether there's just one word or a whole sentence in the label.

Asciiom
  • 9,867
  • 7
  • 38
  • 57
haansi
  • 5,470
  • 21
  • 63
  • 91

8 Answers8

346

Set Label's AutoSize property to False, TextAlign property to MiddleCenter and Dock property to Fill.

decyclone
  • 30,394
  • 6
  • 63
  • 80
  • 1
    thanks decyclone, it worked. can u plz guide what Dock does ? – haansi Dec 03 '10 at 10:38
  • 3
    With `Control.Dock` property (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx), you can dock (stick) a control to a certain side of the container. For example `Left`, `Top`, `Right` or `Bottom`. `Fill` tells the control to take all the available space in the container. – decyclone Dec 03 '10 at 10:42
  • 1
    Unless you want the label to fill the entire space available to it, you may wish to set the Dock property to "None" as suggested by user3866622 in his/her programmatic solution. – Tim Apr 07 '15 at 14:56
  • 4
    Worked, but I didn't need to do anything to the `Dock` setting. – Jim Fell May 20 '16 at 20:42
  • 5
    `Dock` setting was unnecessary. Changing the `AutoSize` property gives the label a bounding box that you can manually adjust the size of. – Nick Roberts Nov 04 '16 at 15:54
  • Anchors can be used instead of dock as well. – Robert J Jul 03 '20 at 13:19
16

You will achive it with setting property Anchor: None.

16

Some minor additional content for setting programmatically:

Label textLabel = new Label() { 
        AutoSize = false, 
        TextAlign = ContentAlignment.MiddleCenter, 
        Dock = DockStyle.None, 
        Left = 10, 
        Width = myDialog.Width - 10
};            

Dockstyle and Content alignment may differ from your needs. For example, for a simple label on a wpf form I use DockStyle.None.

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
user3866622
  • 189
  • 1
  • 2
11

If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.

private void lblReport_SizeChanged(object sender, EventArgs e)
{
    lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
}
Umut OVECOGLU
  • 154
  • 1
  • 6
7

The accepted answer didn't work for me for two reasons:

  1. I had BackColor set so setting AutoSize = false and Dock = Fill causes the background color to fill the whole form
  2. I couldn't have AutoSize set to false anyway because my label text was dynamic

Instead, I simply used the form's width and the width of the label to calculate the left offset:

MyLabel.Left = (this.Width - MyLabel.Width) / 2;
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50
1

I wanted to do something similar, but on a form with a background image, I found that when the text in the label changed the repaints were obvious with this method, so I did the following: * Set the label AutoSize to true and TextAlign to MiddleCenter

Then, each time the text changed (mine was done using a timer) I called the following method:

    private Point GetPosition()
    {
        int y = (this.Height / 2) - (label1.Height / 2);
        int x = (this.Width / 2) - (label1.Width / 2);
        return new Point(x, y);
    }

And set the label's Location property to this return value. This ensured that the label was always in the center of the form when the text changed and the repaints for a full-screen form weren't obvious.

LordPupazz
  • 619
  • 3
  • 15
1

You could try out the following code snippet:

private Point CenterOfMenuPanel<T>(T control, int height=0) where T:Control {
    Point center = new Point( 
        MenuPanel.Size.Width / 2 - control.Width * 2,
        height != 0 ? height : MenuPanel.Size.Height / 2 - control.Height / 2);

    return center;
}

It's Really Center

enter image description here

Wyck
  • 10,311
  • 6
  • 39
  • 60
Minwoo Yu
  • 360
  • 2
  • 13
0

'this.' being the form you are on with lblName as the item you wish to centre. Additionally 'offsetInt' will allow you to position the label left or right of the centre. lblName.Location.Y maintains the existing Y height on the form.

lblName.Location = new Point((int)((this.Width - lblName.Width) / 2) +/- offsetInt, lblName.Location.Y);    
LanceF
  • 25
  • 6