.NET Label.TextAlign seems not to work

20,607

The TextAlign property controls how the text is aligned within the label:

Gets or sets the alignment of text in the label.

If you change the length of the text you need recalculate the Location of the label which is always top left.

I've found this Code Project article which, while probably over the top for what you want, states:

Moreover, if you are going to change the label text (e.g., when localizing the application) or text alignment, you'll have to resize/reposition controls. Therefore, I have created this simple label that takes care of such details.

(my bold)

So you could use the same algorithm to reposition your label.

Share:
20,607
Admin
Author by

Admin

Updated on July 29, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a label that I want to right align to be able to place aligned to a text box.
    The Designer in Visual Studio 2010 Express have generated this code for me

    this.lblAddData.AutoSize = true;  
    this.lblAddData.Location = new System.Drawing.Point(167, 452);  
    this.lblAddData.Name = "lblAddData";  
    this.lblAddData.Size = new System.Drawing.Size(25, 14);  
    this.lblAddData.TabIndex = 5;  
    this.lblAddData.Text = "text";  
    this.lblAddData.TextAlign = System.Drawing.ContentAlignment.MiddleRight;  
    

    [text][textbox] In the code I change the text programmaticly

    lblAddData.Text = "a very long text";
    

    but the text is hiding behind the texbox that I have placed next to the label on the
    right side.

    [a ver][textbox]

    Have someone experienced the same problem before?
    I know it is missing information so ask me if you need more info.
    Best Regards
    Görgen

  • mistertodd
    mistertodd about 13 years
    /facepalm Really Microsoft? Really?