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
Constructor Description FlowLayoutPanel() This Constructors is used to initialize a new instance of the FlowLayoutPanel class. Properties
Property Description AutoScroll This 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. AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents. AutoSizeMode This property indicates the automatic sizing behavior of the control. BackColor This property is used to get or set the background color for the control. BorderStyle This property indicates the border style for the control. FlowDirection This property is used to get or set a value indicating the flow direction of the FlowLayoutPanel control. Font This property is used to get or set the font of the text displayed by the control. ForeColor This property is used to get or set the foreground color of the control. Height This property is used to get or set the height of the control. Location This 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. Name This property is used to get or set the name of the control. Padding This property is used to get or set padding within the control. Size This property is used to get or set the height and width of the control. Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed. Width This property is used to get or set the width of the control. WrapContents This property is used to get or set a value indicating whether the FlowLayoutPanel control should wrap its contents or let the contents be clipped.