How to set the Auto Size Mode of FlowLayoutPanel in C#?

In Windows Forms, FlowLayoutPanel control is used to arrange its child controls in a horizontal or vertical flow direction. Or in other words, FlowLayoutPanel is a container which is used to organize different or same types of controls in it either horizontally or vertically. In FlowLayoutPanel control, you can set a value which indicates how FlowLayoutPanel behaves when the value of the AutoSize Property is set to true, using AutoSizeMode Property. This property has two different values that are defined under AutoSizeMode enum and the values are:

  • GrowOnly: This value indicates that the FlowLayoutPanel grow according to the content, but does not shrink if the content is less.
  • GrowAndShrink: This value indicates the FlowLayoutPanel grows and shrink according to the content present in it.

The default value of this property is GrowOnly. You can set this property in two different ways:

1. Design-Time: It is the easiest way to set the AutoSizeMode property of the FlowLayoutPanel as shown in the following steps:



  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Next, drag and drop the FlowLayoutPanel control from the toolbox to the form as shown in the below image:

  • Step 3: After drag and drop you will go to the properties of the FlowLayoutPanel and set the AutoSizeMode property of the FlowLayoutPanel as shown in the below image:

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the AutoSizeMode property of the FlowLayoutPanel control programmatically with the help of given syntax:

public virtual System.Windows.Forms.AutoSizeMode AutoSizeMode { get; set; }

Here, AutoSizeMode represents the size modes of the FlowLayoutPanel control. It will throw an InvalidEnumArgumentException if the value of this property does not belong to AutoSizeMode enum values. The following steps show how to set the AutoSizeMode property of the FlowLayoutPanel dynamically:

  • Step 1: Create a FlowLayoutPanel using the FlowLayoutPanel() constructor is provided by the FlowLayoutPanel class.
    // Creating a FlowLayoutPanel
    FlowLayoutPanel f = new FlowLayoutPanel();
    
  • Step 2: After creating FlowLayoutPanel, set the AutoSizeMode property of the FlowLayoutPanel provided by the FlowLayoutPanel class.
    // Setting the AutoSizeMode property
     f.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    
  • Step 3: And last add this FlowLayoutPanel control to the form and also adding child controls in the FlowLayoutPanel using the following statements:
    // Adding a FlowLayoutPanel
    // control to the form
    this.Controls.Add(f);
    
    and 
    
    // Adding child controls to
    // the FlowLayoutPanel
    f.Controls.Add(r1);
    

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
  
namespace WindowsFormsApp50 {
  
public partial class Form1 : Form {
  
    public Form1()
    {
        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e)
    {
        // Creating and setting the 
        // properties of FlowLayoutPanel
        FlowLayoutPanel f = new FlowLayoutPanel();
        f.Location = new Point(380, 124);
        f.AutoSize = true;
        f.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        f.Name = "Mycontainer";
        f.Font = new Font("Calibri", 12);
        f.FlowDirection = FlowDirection.RightToLeft;
        f.BorderStyle = BorderStyle.Fixed3D;
        f.ForeColor = Color.BlueViolet;
        f.BackColor = Color.LightPink;
        f.Visible = true;
  
        // Adding this control to the form
        this.Controls.Add(f);
  
        // Creating and setting the
        // properties of radio buttons
        RadioButton r1 = new RadioButton();
        r1.Location = new Point(3, 3);
        r1.Size = new Size(95, 20);
        r1.Text = "R1";
  
        // Adding this control 
        // to the FlowLayoutPanel
        f.Controls.Add(r1);
  
        RadioButton r2 = new RadioButton();
        r2.Location = new Point(94, 3);
        r2.Size = new Size(95, 20);
        r2.Text = "R2";
  
        // Adding this control 
        // to the FlowLayoutPanel
        f.Controls.Add(r2);
  
        RadioButton r3 = new RadioButton();
        r3.Location = new Point(3, 26);
        r3.Size = new Size(95, 20);
        r3.Text = "R3";
  
        // Adding this control 
        // to the FlowLayoutPanel
        f.Controls.Add(r3);
    }
}
}

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.