C#TableLayoutPanel 网格布局

一、网格布局核心基础知识点

1. 什么是网格布局

TableLayoutPanel 是 WinForm 中的网格表格布局容器,类似 Excel 表格,严格按照「行、列」单元格排布控件。

对比流式布局:

1、FlowLayoutPanel(流式):自动从左到右排布,自动换行,无固定单元格

2、TableLayoutPanel(网格):固定行列矩阵,控件精准落在指定单元格,排版更规整、适合整齐表单界面

2. 核心核心属性(必考)

ColumnCount:设置总列数

RowCount:设置总行数

ColumnStyles:列样式集合,控制每一列的宽度占比/尺寸

RowStyles:行样式集合,控制每一行的高度占比/尺寸

SizeType.Percent:百分比尺寸,自动适配窗体大小

DockStyle.Fill:控件填充整个单元格

Controls.Add(控件, 列索引, 行索引):指定行列位置添加控件

3. 索引规则

行列索引从0开始,0代表第一行/第一列,依次递增


二、代码整体结构说明

本案例包含三套网格布局写法(两套注释案例 + 一套最终完整版案例),层层递进:

案例1:固定行列百分比均分布局 + 手动定点添加按钮

案例2:滚动自适应网格布局 + 循环批量生成标签

案例3(最终生效):4列3行全屏填充网格 + 混合控件布局(标签、按钮、文本框)


三、案例一:基础均分网格布局(注释代码精讲)

功能:2列3行固定百分比均分,手动指定行列添加按钮

复制代码
/*
    //1、实例化网格布局容器
    TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();

    //2、设置总列数:2列
    tableLayoutPanel1.ColumnCount = 2;

    //3、设置每列宽度百分比,两列各占50%
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); 
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));

    //4、设置总行数:3行
    tableLayoutPanel1.RowCount = 3;

    //5、设置每行高度百分比,三行均分各占33%
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33F));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33F));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33F));

    //6、定点添加控件 参数:(控件,列,行)
    Button button1 = new Button();
    button1.Text = "Button 1";
    tableLayoutPanel1.Controls.Add(button1, 0, 0);//第0列第0行

    Button button2 = new Button();
    button2.Text = "Button 2";
    tableLayoutPanel1.Controls.Add(button2, 1, 0);//第1列第0行

    Button button3 = new Button();
    button3.Text = "Button 3";
    tableLayoutPanel1.Controls.Add(button3, 1, 1);//第1列第1行

    Button button4 = new Button();
    button4.Text = "Button 4";
    tableLayoutPanel1.Controls.Add(button4, 0, 2);//第0列第2行

    //7、将网格容器添加到主窗体
    this.Controls.Add(tableLayoutPanel1);
*/

案例核心解析

1、通过 Percent 百分比设置行列,窗体缩放时单元格自动自适应

2、Add(控件,列,行) 精准定位控件位置,是网格布局独有的核心方法

3、行列总数必须和 Styles 数量匹配,否则布局错乱


四、案例二:滚动自适应网格布局(注释代码精讲)

功能:实现网格滚动、自动缩放、批量循环生成控件,适合控件过多的场景

复制代码
/*
    TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
    tableLayoutPanel1.ColumnCount = 2;  //2列
    tableLayoutPanel1.RowCount = 10;   //10行

    //开启自动滚动:内容超出容器自动出现滚动条
    tableLayoutPanel1.AutoScroll = true;

    //滚动边距:滚动区域上下左右额外留白20像素
    tableLayoutPanel1.AutoScrollMargin = new Size(20, 20);

    //最小滚动尺寸:限定滚动范围高度为容器2倍
    tableLayoutPanel1.AutoScrollMinSize = new Size(0, tableLayoutPanel1.Height * 2);

    //循环批量创建20个标签
    for (int i = 1; i <= 20; i++)
    {
        Label label = new Label();
        label.Text = "Label " + i.ToString();
        //默认从第0行第0列依次排布
        tableLayoutPanel1.Controls.Add(label);
    }

    //开启自动大小自适应
    tableLayoutPanel1.AutoSize = true;
    tableLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;

    //容器加载到窗体
    this.Controls.Add(tableLayoutPanel1);
 */

滚动布局专属属性解析

AutoScroll = true:开启滚动功能,内容溢出自动显示滚动条

AutoScrollMargin:滚动区域额外留白,防止控件紧贴滚动边界

AutoSizeMode.GrowAndShrink:容器随控件多少自动放大缩小

无指定行列添加控件时,默认从左到右、从上到下自动填充单元格


五、案例三:最终完整版 4×3 全屏网格布局(生效代码逐行精讲)

功能:4列3行全屏填充、百分比均分、混合放置标签/按钮/文本框、居中显示、彩色区分

复制代码
private void Form1_Load(object sender, EventArgs e)
{
    //1、实例化网格布局容器
    TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
             
    //2、设置容器填充整个窗体
    tableLayoutPanel1.Dock = DockStyle.Fill;

    //3、定义网格规格:4列3行
    tableLayoutPanel1.ColumnCount = 4;
    tableLayoutPanel1.RowCount = 3;

    //4、四列均分宽度,每列占25%
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F));

    //5、三行均分高度,每行占33.33%
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33F));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33F));
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33F));

    //6、将网格容器添加到主窗体
    this.Controls.Add(tableLayoutPanel1);

第一行(第0行):彩色居中标签控件

复制代码
    //第0列第0行 红色标签
    Label label1 = new Label { 
        Text = "Label 1", 
        Dock = DockStyle.Fill, //填充整个单元格
        TextAlign = ContentAlignment.MiddleCenter, //文字居中
        BackColor = Color.Red 
    };
    tableLayoutPanel1.Controls.Add(label1, 0, 0);

    //第1列第0行 蓝色标签
    Label label2 = new Label { 
        Text = "Label 2", 
        Dock = DockStyle.Fill, 
        TextAlign = ContentAlignment.MiddleCenter, 
        BackColor = Color.Blue 
    };
    tableLayoutPanel1.Controls.Add(label2, 1, 0);

    //第2列第0行 绿色标签
    Label label3 = new Label { 
        Text = "Label 3", 
        Dock = DockStyle.Fill, 
        TextAlign = ContentAlignment.MiddleCenter, 
        BackColor = Color.Green 
    };
    tableLayoutPanel1.Controls.Add(label3, 2, 0);

    //第3列第0行 黄色标签
    Label label4 = new Label { 
        Text = "Label 4", 
        Dock = DockStyle.Fill, 
        TextAlign = ContentAlignment.MiddleCenter, 
        BackColor = Color.Yellow 
    };
    tableLayoutPanel1.Controls.Add(label4, 3, 0);

第二行(第1行):填充按钮控件

复制代码
    //第0-3列 第二行放置按钮
    Button button1 = new Button { Text = "Button 1", Dock = DockStyle.Fill };
    tableLayoutPanel1.Controls.Add(button1, 0, 1);

    Button button2 = new Button { Text = "Button 2", Dock = DockStyle.Fill };
    tableLayoutPanel1.Controls.Add(button2, 1, 1);

    Button button3 = new Button { Text = "Button 3", Dock = DockStyle.Fill };
    tableLayoutPanel1.Controls.Add(button3, 2, 1);

    Button button4 = new Button { Text = "Button 4", Dock = DockStyle.Fill };
    tableLayoutPanel1.Controls.Add(button4, 3, 1);

第三行(第2行):文本框控件

复制代码
    //第0列第三行放置文本框
    TextBox textBox1 = new TextBox { Dock = DockStyle.Fill };
    tableLayoutPanel1.Controls.Add(textBox1, 0, 2);
}   

六、核心属性超精细总结

1、尺寸类型 SizeType

Percent:百分比布局,自适应窗体,最常用

Absolute:固定像素尺寸

AutoSize:自动适配内容大小

2、控件排版属性

DockStyle.Fill:控件铺满整个单元格,界面整齐无留白

ContentAlignment.MiddleCenter:文本水平垂直居中

3、控件挂载规则

语法:容器.Controls.Add(控件, 列索引, 行索引);

必须指定行列,否则默认自动排布,无法精准定位


七、三套案例功能对比总结

案例1:基础固定网格,适合少量固定控件规整排版

案例2:滚动自适应网格,适合大量动态生成控件

案例3:全屏均分混合布局,适合表单、后台界面、整齐功能面板


八、易错点必考总结

1、设置 ColumnCount / RowCount 后,必须对应添加同等数量 ColumnStyles / RowStyles,否则布局错乱

2、行列索引从0开始,写索引越界会直接报错

3、不设置 DockStyle.Fill,控件默认大小不一,排版混乱

4、动态容器必须调用 this.Controls.Add() 才能显示在窗体上

5、百分比总和必须接近100%,避免留白或超出屏幕

6、网格布局优先固定行列,比流式布局更适合规整表单界面