Articles in this section
Category / Section

How to provide padding to the sides of the layout rect in WinForms CardLayout?

2 mins read

Padding

While the default "layout rect" is the whole client rectangle of the container control, you can provide some padding to the sides by specifying a different layout rect via the CustomLayoutBounds property. But if your container is resizable (most probably), then this property’s value should keep changing as the container’s bounds change.

You can do so by first turning off AutoLayout on the manager, listening to the container’s layout event, setting the new layout rect from within the handler, and then manually triggering the layout, as follows:

C#

// Turn off AutoLayout on the layout manager and trigger the layout manually, since we will provide custom layout bounds, below.
// Listen to the container’s Layout event and do the following:
private void panel1_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
   // If the container control is getting resized...
   if(e.AffectedControl == this.panel1)
   {
      // Give some padding to the sides of the layout rect.
      Rectangle clientRectangle = this.panel1.ClientRectangle;
      clientRectangle.Inflate(-40, -40);
      // Provide the custom layout bounds and trigger the layout.    
      this.cardLayout1.CustomLayoutBounds = clientRectangle;
      this.cardLayout1.LayoutContainer();
   }
}

VB

' Turn off AutoLayout on the layout manager and trigger the layout manually, since we will provide custom layout bounds, below.
' Listen to the container’s Layout event and do the following:
Private  Sub panel1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs
   ' If the container control is getting resized...
   If e.AffectedControl = Me.panel1 Then
      ' Give some padding to the sides of the layout rect.
      Dim clientRectangle As Rectangle = Me.panel1.ClientRectangle
      clientRectangle.Inflate(-40, -40)
      ' Provide the custom layout bounds and trigger the layout.
      Me.cardLayout1.CustomLayoutBounds = clientRectangle
      Me.cardLayout1.LayoutContainer()
   End If
End Sub

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied