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

 

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

Article ID: Published Date: Last Revised Date: Platform: Control:
1180 04/30/2011 07/10/2014 WinForms XPToolbar
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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