📌  相关文章
📜  如何在C#中设置FlowLayoutPanel的自动大小模式?

📅  最后修改于: 2021-05-30 00:51:41             🧑  作者: Mango

在Windows窗体中,FlowLayoutPanel控件用于在水平或垂直流方向上排列其子控件。换句话说,FlowLayoutPanel是一个容器,用于在其中水平或垂直地组织不同或相同类型的控件。在FlowLayoutPanel控件中,您可以使用AutoSizeMode属性设置一个值,该值指示当AutoSize属性的值设置为true时FlowLayoutPanel的行为。此属性具有两个不同的值,这些值在AutoSizeMode枚举下定义,分别为:

  • GrowOnly:此值指示FlowLayoutPanel根据内容增长,但如果内容较少,则不会收缩。
  • GrowAndShrink:此值指示FlowLayoutPanel根据其中的内容而增大和缩小。

此属性的默认值为GrowOnly。您可以通过两种不同的方式设置此属性:

1.设计时:这是设置FlowLayoutPanel的AutoSizeMode属性的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将FlowLayoutPanel控件从工具箱拖放到窗体,如下图所示:

  • 步骤3:拖放之后,您将转到FlowLayoutPanel的属性并设置FlowLayoutPanel的AutoSizeMode属性,如下图所示:

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置FlowLayoutPanel控件的AutoSizeMode属性:

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

在这里,AutoSizeMode表示FlowLayoutPanel控件的尺寸模式。如果此属性的值不属于AutoSizeMode枚举值,它将抛出InvalidEnumArgumentException。以下步骤显示如何动态设置FlowLayoutPanel的AutoSizeMode属性:

  • 步骤1:使用FlowLayoutPanel类提供的FlowLayoutPanel()构造函数创建FlowLayoutPanel。
    // Creating a FlowLayoutPanel
    FlowLayoutPanel f = new FlowLayoutPanel();
    
  • 步骤2:创建FlowLayoutPanel后,设置FlowLayoutPanel类提供的FlowLayoutPanel的AutoSizeMode属性。
    // Setting the AutoSizeMode property
     f.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    
  • 步骤3:最后,将此FlowLayoutPanel控件添加到表单中,并使用以下语句在FlowLayoutPanel中添加子控件:
    // Adding a FlowLayoutPanel
    // control to the form
    this.Controls.Add(f);
    
    and 
    
    // Adding child controls to
    // the FlowLayoutPanel
    f.Controls.Add(r1);
    

例子:

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

输出: