)
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: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: FlowLayout |
Published Date: February 7, 2020 |
Last Revised Date: February 7, 2020

Use the FlowLayout to autosize the label

The 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 or 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

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

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
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.

Up arrow icon

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

Live Chat Icon For mobile
Live Chat Icon