动态生成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);
}
}