views:

48

answers:

2

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

A: 

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.

ChrisF
+1  A: 

Well, I spotted the error myself; AutoSize was set to true, that is default behavour

this.lblAddData.AutoSize = true;

When I changed this to false it worked as I assumed it should.

Gorgen