C# FlowLayoutPanel类用法介绍

2021年5月9日16:36:10 发表评论 2,764 次浏览

在Windows窗体中, FlowLayoutPanel控件用于在水平或垂直流方向上排列其子控件。换句话说, FlowLayoutPanel是一个容器, 用于在其中水平或垂直地组织不同或相同类型的控件。 FlowLayoutPanel类用于表示Windows流布局面板, 还提供不同类型的属性, 方法和事件。它在下定义System.Windows.Forms命名空间。在C#中, 可以使用两种不同的方法在Windows窗体中创建FlowLayoutPanel:

1.设计时间:这是创建FlowLayoutPanel控件的最简单方法, 如以下步骤所示:

第1步:

创建一个Windows窗体, 如下图所示:

Visual Studio->文件->新建->项目-> WindowsFormApp

C# FlowLayoutPanel类用法介绍1

第2步:

接下来, 将FlowLayoutPanel控件从工具箱拖放到窗体, 如下图所示:

C# FlowLayoutPanel类用法介绍2

第三步:

拖放后, 你将转到FlowLayoutPanel的属性以根据需要修改FlowLayoutPanel。

C# FlowLayoutPanel类用法介绍3

输出如下:

C# FlowLayoutPanel类用法介绍4

2.运行时:它比上面的方法有些棘手。在此方法中, 你可以借助FlowLayoutPanel类提供的语法以编程方式创建FlowLayoutPanel。以下步骤显示如何动态设置创建FlowLayoutPanel:

步骤1:使用FlowLayoutPanel类提供的FlowLayoutPanel()构造函数创建FlowLayoutPanel。 //创建一个FlowLayoutPanel FlowLayoutPanel fl = new FlowLayoutPanel();

步骤2:创建FlowLayoutPanel后, 设置FlowLayoutPanel类提供的FlowLayoutPanel的属性。 //设置FlowLayoutPanel的位置fl.Location = new Point(380, 124); //设置FlowLayoutPanel的大小fl.Size = new Size(216, 57); //设置FlowLayoutPanel的名称fl.Name =" Mycontainer"; //设置FlowLayoutPanel的字体fl.Font = new Font(" Calibri", 12); //设置FlowLayoutPanel的流向fl.FlowDirection = FlowDirection.RightToLeft; //设置FlowLayoutPanel的边框样式fl.BorderStyle = BorderStyle.Fixed3D; //设置FlowLayoutPanel的前景色fl.ForeColor = Color.BlueViolet; //设置FlowLayoutPanel的可见性fl.Visible = true;

第三步:

最后, 将此FlowLayoutPanel控件添加到表单中, 并使用以下语句在FlowLayoutPanel上添加其他控件:

//Adding a FlowLayoutPanel
//control to the form
this.Controls.Add(fl);
and 

//Adding child controls 
//to the FlowLayoutPanel
fl.Controls.Add(f1);

例子:

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);
     }
}
}

输出如下:

C# FlowLayoutPanel类用法介绍5

构造器

构造器 描述
FlowLayoutPanel() 此构造方法用于初始化FlowLayoutPanel类的新实例。

属性

属性 描述
AutoScroll 此属性用于获取或设置一个值, 该值指示容器是否使用户能够滚动到位于其可见边界之外的任何控件。
AutoSize 此属性用于获取或设置一个值, 该值指示控件是否根据其内容调整大小。
AutoSizeMode 此属性指示控件的自动调整大小行为。
BackColor 此属性用于获取或设置控件的背景色。
BorderStyle 此属性指示控件的边框样式。
FlowDirection 此属性用于获取或设置一个值, 该值指示FlowLayoutPanel控件的流向。
Font 此属性用于获取或设置控件显示的文本的字体。
ForeColor 此属性用于获取或设置控件的前景色。
Height 此属性用于获取或设置控件的高度。
Location 此属性用于获取或设置FlowLayoutPanel控件的左上角相对于其窗体的左上角的坐标。
Name 此属性用于获取或设置控件的名称。
Padding 此属性用于获取或设置控件内的填充。
Size 此属性用于获取或设置控件的高度和宽度。
Visible 此属性用于获取或设置一个值, 该值指示是否显示该控件及其所有子控件。
Width 此属性用于获取或设置控件的宽度。
WrapContents 此属性用于获取或设置一个值, 该值指示FlowLayoutPanel控件是应该包装其内容还是剪切内容。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: