📌  相关文章
📜  C#| FlowLayoutPanel类

📅  最后修改于: 2021-05-29 17:25:07             🧑  作者: Mango

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

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

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将FlowLayoutPanel控件从工具箱拖放到窗体,如下图所示:
  • 步骤3:拖放之后,您将转到FlowLayoutPanel的属性,以根据需要修改FlowLayoutPanel。

    输出:

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

  • 步骤1:使用FlowLayoutPanel类提供的FlowLayoutPanel()构造函数创建FlowLayoutPanel。
    // Creating a FlowLayoutPanel
    FlowLayoutPanel fl = new FlowLayoutPanel(); 
    
  • 步骤2:创建FlowLayoutPanel后,设置FlowLayoutPanel类提供的FlowLayoutPanel的属性。
    // 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; 
    
  • 步骤3:最后,将此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);
        }
    }
    }
    

    输出:

    建设者

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

    特性

    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.