We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to use the FlowLayout to autosize the label control on a form

Platform: WinForms |
Control: XPToolbar |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014


The attached sample shows how FlowLayout can be used to provide autosizing behavior for a label control on a form. The .NET label control can now expand/collapse horizontally as the form width is increased/decreased.

To set up this behavior:

- Set the Minimumsize property for the form.

- For the FlowLayout component, set the LayoutMode to Vertical and provide LeftMargin and RightMargin.

- To autosize the label with the text, you need to listen to the FlowLayout's ProvideLayoutInformation event to set the PreferredSize for the label control. You have to do this because the .NET label control does not have a concept of Preferredsize.

[C#] private void flowLayout1_ProvideLayoutInformation(object sender, Syncfusion.Windows.Forms.Tools.ProvideLayoutInformationEventArgs e) {  if (e.Control == this.label1 && e.Requested == LayoutInformationType.PreferredSize)  {   Graphics g = this.CreateGraphics();   SizeF szPref = g.MeasureString(this.label1.Text, this.label1.Font, this.ClientRectangle.Width);   e.Size = new Size(this.ClientRectangle.Width-20, (int)szPref.Height + 5);    e.Handled = true;   g.Dispose();  }


[VB.NET] Private  Sub flowLayout1_ProvideLayoutInformation(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Tools.ProvideLayoutInformationEventArgs)  If e.Control = Me.label1 And e.Requested = LayoutInformationType.PreferredSize Then   Dim g As Graphics =  Me.CreateGraphics()   Dim szPref As SizeF =  g.MeasureString(Me.label1.Text,Me.label1.Font,Me.ClientRectangle.Width)   e.Size = New Size(Me.ClientRectangle.Width-20, CType(szP.Height + 5, Integer))    e.Handled = True   g.Dispose()  End If   End Sub

You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon