Skip to content
Related Articles

Related Articles

C# | FlowLayoutPanel Class
  • Last Updated : 29 Aug, 2019

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. The FlowLayoutPanel class is used to represent windows flow layout panel and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. In C#, you can create a FlowLayoutPanel in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a FlowLayoutPanel control 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 to modify FlowLayoutPanel according to your requirement.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a FlowLayoutPanel programmatically with the help of syntax provided by the FlowLayoutPanel class. The following steps show how to set the create FlowLayoutPanel dynamically:

  • Step 1: Create a FlowLayoutPanel using the FlowLayoutPanel() constructor is provided by the FlowLayoutPanel class.
    // Creating a FlowLayoutPanel
    FlowLayoutPanel fl = new FlowLayoutPanel(); 
    
  • Step 2: After creating a FlowLayoutPanel, set the property of the FlowLayoutPanel provided by the FlowLayoutPanel class.
    // Setting the location of the FlowLayoutPanel
     fl.Location = new Point(380, 124); 
    
    // Setting the size of the FlowLayoutPanel
            fl.Size = new Size(216, 57); 
    
    // Setting the name of the FlowLayoutPanel
            fl.Name = "Mycontainer"; 
    
    // Setting the font of the FlowLayoutPanel
            fl.Font = new Font("Calibri", 12); 
    
    // Setting the flow direction of the FlowLayoutPanel
            fl.FlowDirection = FlowDirection.RightToLeft; 
    
    // Setting the border style of the FlowLayoutPanel
            fl.BorderStyle = BorderStyle.Fixed3D; 
    
    // Setting the foreground color of the FlowLayoutPanel
            fl.ForeColor = Color.BlueViolet; 
    
    // Setting the visibility of the FlowLayoutPanel
            fl.Visible = true; 
    
  • Step 3: And last add this FlowLayoutPanel control to the form and also add other controls on the FlowLayoutPanel using the following statements:
    // Adding a FlowLayoutPanel
    // control to the form
    this.Controls.Add(fl);
    and 
    
    // Adding child controls 
    // to the FlowLayoutPanel
    fl.Controls.Add(f1);
    

    Example:




    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 fl = new FlowLayoutPanel();
            fl.Location = new Point(380, 124);
            fl.Size = new Size(216, 57);
            fl.Name = "Myflowpanel";
            fl.Font = new Font("Calibri", 12);
            fl.FlowDirection = FlowDirection.RightToLeft;
            fl.BorderStyle = BorderStyle.Fixed3D;
            fl.ForeColor = Color.BlueViolet;
            fl.Visible = true;
      
            // Adding this control to the form
            this.Controls.Add(fl);
      
            // Creating and setting the
            // properties of radio buttons
            RadioButton f1 = new RadioButton();
            f1.Location = new Point(3, 3);
            f1.Size = new Size(95, 20);
            f1.Text = "R1";
      
            // Adding this control
            // to the FlowLayoutPanel
            fl.Controls.Add(f1);
      
            RadioButton f2 = new RadioButton();
            f2.Location = new Point(94, 3);
            f2.Size = new Size(95, 20);
            f2.Text = "R2";
      
            // Adding this control
            // to the FlowLayoutPanel
            fl.Controls.Add(f2);
      
            RadioButton f3 = new RadioButton();
            f3.Location = new Point(3, 26);
            f3.Size = new Size(95, 20);
            f3.Text = "R3";
      
            // Adding this control
            // to the FlowLayoutPanel
            fl.Controls.Add(f3);
        }
    }
    }

    Output:

    Constructor

    ConstructorDescription
    FlowLayoutPanel()This Constructors is used to initialize a new instance of the FlowLayoutPanel class.

    Properties

    PropertyDescription
    AutoScrollThis property is used to get or set a value indicating whether the container enables the user to scroll to any controls placed outside of its visible boundaries.
    AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
    AutoSizeModeThis property indicates the automatic sizing behavior of the control.
    BackColorThis property is used to get or set the background color for the control.
    BorderStyleThis property indicates the border style for the control.
    FlowDirectionThis property is used to get or set a value indicating the flow direction of the FlowLayoutPanel control.
    FontThis property is used to get or set the font of the text displayed by the control.
    ForeColorThis property is used to get or set the foreground color of the control.
    HeightThis property is used to get or set the height of the control.
    LocationThis property is used to get or set the coordinates of the upper-left corner of the FlowLayoutPanel control relative to the upper-left corner of its form.
    NameThis property is used to get or set the name of the control.
    PaddingThis property is used to get or set padding within the control.
    SizeThis property is used to get or set the height and width of the control.
    VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
    WidthThis property is used to get or set the width of the control.
    WrapContentsThis property is used to get or set a value indicating whether the FlowLayoutPanel control should wrap its contents or let the contents be clipped.

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :