Resizing label in Windows Forms application

16,555

Solution 1

You could put them into a TableLayoutPanel with 2 columns and two rows. Each label goes in left side of each row and both combo box/buttons goes in the others cells (right side of each row).

Then you must dock both elements (Dock Fill) and set the columns to AutoSize. (As you can see in the image)

AutoSize Columns

You also may want to dock the TablePanelLayout to your common panel.

As you can see in the image below, both TablePanelLayout have the same components. But in the secoend I just changed the label3's text.

enter image description here

Hope it helps. (Also sorry for my bad english, it isn't my native language. Please feel free to correct any wrong spelling, thanks!)

Solution 2

First suggestion from me would be:

  1. set the AutoSize property to False
  2. Make sure that the width is alot larger than it needs to be by resizing the label.
  3. The label should now fit nicely regardless of what content it receive.

Second suggestion, you can use the GDI+ to determine the size of the text and then resize the label accordingly See http://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx

Graphics gfx = this.label1.CreateGraphics(); // I think its called that, cant remember :)

Font stringFont = new Font("Arial", 16);

// Measure string.
SizeF stringSize = new SizeF();
stringSize = gfx.MeasureString(this.label1.Text, stringFont);
this.label1.Size = new Size((int)stringSize.Width, (int)stringSize.Height);

Oh yeah I almost forgot. Make sure that you're panel is not the cause of the clipping. I mean, check if the panel is large enough for the labels to fit :-)

Hope this helps!

Best regards, Zerratar

Share:
16,555
Rahul Vijay Dawda
Author by

Rahul Vijay Dawda

A software developer by profession and a Manchester United fan.

Updated on June 05, 2022

Comments

  • Rahul Vijay Dawda
    Rahul Vijay Dawda almost 2 years

    I have a Windows Forms application which contains a couple of labels, a button and a combobox all wrapper inside a Panel.

    this.pnlSuboptions.Controls.Add(this.label1);
    this.pnlSuboptions.Controls.Add(this.cboPtSize);
    this.pnlSuboptions.Controls.Add(this.label2);
    this.pnlSuboptions.Controls.Add(this.btnSelect);
    

    I facing an issue with my labels when I try loading localized strings for my labels. The localized strings for some languages are larger than the English strings. In such cases, a part of the label gets hidden under the combo box or the button.

    I want the label to increase in size towards the left instead of right. I've set my labels' AutoSize property to true and also played around with the Anchor property but nothing seems to work.

    I found an SO link which contains a solution to this problem when the label text changes but I'm sure how I can apply this in my scenario where the label is read only once during the form load.

    Any suggestions?