Search code examples
vb.netwinformsautosize

Change the size of a Form depending on the size of child Label


How would you change the Form size depending on the width/length of a Label?

I need to resize a Form depending on how much text there is in a Label (the text is automatically generated from case statements).


Solution

  • You can try to put the AutoSize to True of the Form

    Open the Form and put a Label on it , the Label gets a Name lets say Label1. enter image description here

    Rightclick on the Form and open Properties enter image description here

    Then Choose AutoSize to True

    enter image description here

    Then return to the Form and dubbleclick on the Form this case Form1, Form1.vb is open.

    In Form1_Loadif it is another Label you have to adjust it to the right Name:

      Label1.Text = "This is a example text and a bit longer zo the form is getting bigger"
    

    enter image description here

    Run it and there it is :

    enter image description here

    Look here https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.autosize?view=windowsdesktop-6.0