當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# FlowLayoutPanel用法及代碼示例

在 Windows 窗體中,FlowLayoutPanel 控件用於按水平或垂直流動方向排列其子控件。或者換句話說,FlowLayoutPanel是一個容器,用於水平或垂直組織其中不同或相同類型的控件。 FlowLayoutPanel 類用於表示窗口流布局麵板,並提供不同類型的屬性、方法和事件。它是在 System.Windows.Forms 命名空間下定義的。在 C# 中,您可以使用兩種不同的方式在 Windows 窗體中創建FlowLayoutPanel:

1. Design-Time:這是創建FlowLayoutPanel控件的最簡單方法,步驟如下:

  • 步驟1:創建一個windows窗體,如下圖所示:
    Visual Studio -> 文件 -> 新建 -> 項目 -> WindowsFormApp
    Create a windows form
  • 第2步:接下來,將FlowLayoutPanel控件從工具箱拖放到窗體中,如下圖所示:
    drag and drop the FlowLayoutPanel control
  • 步驟3:拖放後,您將轉到FlowLayoutPanel的屬性,根據您的要求修改FlowLayoutPanel。
    modify FlowLayoutPanel

    輸出:
    FlowLayoutPanel Class

2.運行時:它比上麵的方法稍微棘手一些。在此方法中,您可以借助FlowLayoutPanel 類提供的語法以編程方式創建FlowLayoutPanel。以下步驟顯示如何動態設置創建FlowLayoutPanel:

  • 步驟1:使用以下命令創建FlowLayoutPanelFlowLayoutPanel()構造函數由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); 
        } 
    } 
    } 

    輸出:

    FlowLayoutPanel Class

    Constructor

    構造函數 說明
    FlowLayoutPanel() 此構造函數用於初始化 FlowLayoutPanel 類的新實例。

    Properties

    屬性 說明
    AutoScroll 此屬性用於獲取或設置一個值,該值指示容器是否允許用戶滾動到放置在其可見邊界之外的任何控件。
    AutoSize 此屬性用於獲取或設置一個值,該值指示控件是否根據其內容調整大小。
    AutoSizeMode 此屬性指示控件的自動調整大小行為。
    BackColor 該屬性用於獲取或設置控件的背景顏色。
    BorderStyle 該屬性指示控件的邊框樣式。
    FlowDirection 該屬性用於獲取或設置一個值,該值指示FlowLayoutPanel 控件的流向。
    Font 該屬性用於獲取或設置控件顯示的文本的字體。
    ForeColor 該屬性用於獲取或設置控件的前景色。
    Height 該屬性用於獲取或設置控件的高度。
    Location 該屬性用於獲取或設置 FlowLayoutPanel 控件的左上角相對於其窗體左上角的坐標。
    Name 該屬性用於獲取或設置控件的名稱。
    Padding 此屬性用於獲取或設置控件內的填充。
    Size 該屬性用於獲取或設置控件的高度和寬度。
    Visible 該屬性用於獲取或設置一個值,該值指示是否顯示該控件及其所有子控件。
    Width 該屬性用於獲取或設置控件的寬度。
    WrapContents 此屬性用於獲取或設置一個值,該值指示 FlowLayoutPanel 控件是否應換行其內容或剪切內容。


相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 C# | FlowLayoutPanel Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。