tablelayoutpanel 里面放FlowLayoutPanel

这篇博客展示了如何在C#中动态生成一个TableLayoutPanel,并在其中嵌套FlowLayoutPanel,实现复杂的窗口布局。博主详细解释了代码逻辑,包括设置表格行数、列数、单元格样式以及FlowLayoutPanel的流式布局方向,同时添加了不同颜色的Panel作为示例。代码适用于Windows Forms应用的界面设计,有助于提升UI的定制能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态生成tablelayoutpanel 里面嵌套FlowLayoutPanel的代码。

效果图如下:

思路:创建tablelayoutpanel 为三行一列

每列里面放一个FlowLayoutPanel,FlowLayoutPanel想要几列,就放Panel。

 

代码如下:

    public partial class UCHome : UserControl
    {
        string[] category;//阶梯瀑布图,原因集合, 技术损失 换型损失 组织损失 计划停机 有几个原因数据合计查询
        TableLayoutPanel table = new TableLayoutPanel();
        string categoryStr = string.Empty;
        public UCHome(DateTime dateBeginTemp,
            DateTime dateEndTemp,
            string particlesizeTemp, string[] categoryTemp,
            int parentW, int parentH)
        {
            InitializeComponent();
            category = categoryTemp;
            categoryStr = string.Join(",", category);
            this.Width = parentW;
            this.Height = parentH;


            this.BackColor = Color.White;
            panel1.BackColor = Color.White;
            panel1.Width = this.Width;
            panel1.Height = this.Height;
            DisplayLayoutData();
        }
        private void DisplayLayoutData()
        {

            int totalHeight = this.Height;
            panel1.AutoScroll = true;
            panel1.HorizontalScroll.Visible = true;
            table.AutoScroll = true;
            table.AutoSize = true;
            table.Dock = DockStyle.Fill;     //顶部填充
            table.ColumnCount = 1;
            table.RowCount = 3;

            table.Height = panel1.Height;
            int totalW = panel1.Width; ;
            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, this.Width));

           
            for (int row = 0; row < 3; row++)
            {
                int columnHeight = 0;
                FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel();
                flowLayoutPanel.Width = this.Width;
                if (row == 0)
                {
                    table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, this.Width));
                    columnHeight = 300;
                    RowStyle rowStyle1 = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, this.Width);
                    rowStyle1.Height = columnHeight;
                    table.RowStyles.Add(rowStyle1);
                    // 设置cell样式,增加线条
                    table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
                    flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
                    flowLayoutPanel.Height = columnHeight;

               
                    Panel panoee = new Panel();
                    panoee.Top = 0;
                    panoee.Left = 0;
                    panoee.BackColor = Color.DarkRed;
                    panoee.Name = "panoee";
                    panoee.Size = new System.Drawing.Size(1200, columnHeight);
                    panoee.TabIndex = 0;
                    panoee.Text = "OEE";
                    flowLayoutPanel.Controls.Add(panoee);
                    int ppmWidth = this.Width - 1200 - 30;
                    Panel panelRight = new Panel();
                    panelRight.Top = 1;
                    panelRight.Left = 1200;
                    panelRight.BackColor = Color.White;
                    panelRight.Name = "panelLeft";
                    panelRight.Size = new System.Drawing.Size(ppmWidth, columnHeight);
                    panelRight.TabIndex = 1;
                    panelRight.Text = "panelLeft";

                    int rightHeight = (columnHeight / 2);
                    Panel panelProductivity = new Panel();
                    panelProductivity.Top = 0;
                    panelProductivity.Left = 0;
                    panelProductivity.BackColor = Color.Black;
                    panelProductivity.Name = "panelProductivity";
                    panelProductivity.Size = new System.Drawing.Size(ppmWidth, rightHeight);
                    panelProductivity.TabIndex = 2;
                    panelProductivity.Text = "panelProductivity";
                    panelRight.Controls.Add(panelProductivity);


                    Panel panelPPM = new Panel();
                    panelPPM.Top = rightHeight;
                    panelPPM.Left = 0;
                    panelPPM.BackColor = Color.Red;
                    panelPPM.Name = "panelPPM";
                    panelPPM.Size = new System.Drawing.Size(ppmWidth, rightHeight);
                    panelPPM.TabIndex = 3;
                    panelPPM.Text = "panelPPM";
                    panelRight.Controls.Add(panelPPM);
                    flowLayoutPanel.Controls.Add(panelRight);
                }
                if (row == 1)
                {
                    table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, this.Width));
                    columnHeight = 400;
                    RowStyle rowStyle1 = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, this.Width);
                    rowStyle1.Height = columnHeight;
                    table.RowStyles.Add(rowStyle1);
                    // 设置cell样式,增加线条
                    table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
                    flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
                    flowLayoutPanel.Height = columnHeight;


                    Panel panelPieChart = new Panel();
                    panelPieChart.Top = 0;
                    panelPieChart.Left = 0;
                    panelPieChart.BackColor = Color.Red;
                    panelPieChart.Name = "panelPieChart";
                    panelPieChart.Size = new System.Drawing.Size(400, columnHeight);
                    panelPieChart.TabIndex = 4;
                    panelPieChart.Text = "panelPieChart";
                    flowLayoutPanel.Controls.Add(panelPieChart);


                    Panel panelBarChart = new Panel();
                    panelBarChart.Top = 0;
                    panelBarChart.Left = 0;
                    panelBarChart.BackColor = Color.Blue;
                    panelBarChart.Name = "panelPieChart";
                    panelBarChart.Size = new System.Drawing.Size(400, columnHeight);
                    panelBarChart.TabIndex = 5;
                    panelBarChart.Text = "panelPieChart";
                    flowLayoutPanel.Controls.Add(panelBarChart);


                    int rightWidth = this.Width-800-30;
                    Panel panelBarLineChart = new Panel();
                    panelBarLineChart.Top = 0;
                    panelBarLineChart.Left = 0;
                    panelBarLineChart.BackColor = Color.Yellow;
                    panelBarLineChart.Name = "panelBarLineChart";
                    panelBarLineChart.Size = new System.Drawing.Size(rightWidth, columnHeight);
                    panelBarLineChart.TabIndex = 6;
                    panelBarLineChart.Text = "panelBarLineChart";
                    flowLayoutPanel.Controls.Add(panelBarLineChart);
                }
                if (row == 2)
                {
                    table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, this.Width));
                    columnHeight = totalHeight-700-20;
                    RowStyle rowStyle1 = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, this.Width);
                    rowStyle1.Height = columnHeight;
                    table.RowStyles.Add(rowStyle1);
                    // 设置cell样式,增加线条
                    table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
                    flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
                    flowLayoutPanel.Height = columnHeight;


                    Panel panelLeft = new Panel();
                    panelLeft.Top = 0;
                    panelLeft.Left = 0;
                    panelLeft.BackColor = Color.GreenYellow;
                    panelLeft.Name = "panelLeft";
                    panelLeft.Size = new System.Drawing.Size(1000, columnHeight);
                    panelLeft.TabIndex = 7;
                    panelLeft.Text = "panelLeft";
                    flowLayoutPanel.Controls.Add(panelLeft);

                    int rightWidth = this.Width - 1000 - 30;
                    Panel panelRight = new Panel();
                    panelRight.Top = 0;
                    panelRight.Left = 1000;
                    panelRight.BackColor = Color.Black;
                    panelRight.Name = "panelRight";
                    panelRight.Size = new System.Drawing.Size(rightWidth, columnHeight);
                    panelRight.TabIndex = 8;
                    panelRight.Text = "panelRight";
                    flowLayoutPanel.Controls.Add(panelRight);
                }
                table.Controls.Add(flowLayoutPanel,0, row);
            }
            this.Width = totalW;
            panel1.Controls.Add(table);
        }

}

 

FlowLayoutPanel 一些应用程序需要一个布局可随窗体大小的调整或其中内容大小的改变而自动进行适当排列的窗体。在需要动态布局并且不希望在代码中显式处理 Layout 事件时,可考虑使用布局面板。 FlowLayoutPanel.NET Framework的新增控件。顾名思义,面板可以采用Web窗体的方式给Windows窗体布局。FlowLayoutPanel是一个容器,允许以垂直或水平的方式置包含的控件。除了置控件之外,还可以剪辑控件。置的方向使用FlowDirection属性和FlowDirection枚举来设置。WrapContents属性确定在重新设置窗体的大小时,控件是在下一行、下一列,还是剪辑控件。 FlowLayoutPanel 按特定的流方向排列其内容:水平或垂直。其内容可从一行换到下一行,或者从一列换到下一列。另一种情况是不换行,而是将其内容截掉。 相信大家在做WinForm项目的时候,要对大量的控件进行排序(位置摆),这个容器肯定最受欢迎,但很遗憾的是,此容器本身虽支持Dock和Anchor属性,但不支持入此容器内的控件的Dock和Anchor属性(自动调整宽度),也就说,但窗体伸缩,FlowLayoutPanel容器自身可以缩,但是里面的控件就没那么幸运了,不支持自动缩,这样就必须写方法来触发新的事件来调整控件的大小,这样就会导致窗体的闪烁(重绘)。 借助ManagedSpy工具,我们可以看到此容器里面的器件的结构,我们可以在Form1里面添加一个事件SizeChanged 对容器里面每个器件重新给它大小 就行了。 附件:FlowLayoutPanel的Demo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值