我以dinamically方式创建tableLayoutPannel的行和列。我正确创建了8行和2列(在创建行和列后使用断点进行验证)。但是,屏幕的位置不是我想要的(见截图)。我想将tableLayoutPannel嵌入底部的splitContainer(splitContainer.Panel2)中。我还希望它能完全垂直和水平填充它。我怎么能这样做?
这是UI元素的层次结构:
用于生成行和列的代码:
tlpCharts.ColumnCount = 2;
tlpCharts.RowCount = _numChannels;
for (int chIdx = 0; chIdx < _numChannels; chIdx++)
{
tlpCharts.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
tlpCharts.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
tlpCharts.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 90F));
var cbEnableChannel = new CheckBox();
// customize chEnableChannel
var tmpChart = new Chart();
//customize tmpChart
tmpChart.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left);
// Add chart and checkbox
tlpCharts.Controls.Add(tmpChart, 0, chIdx})
tlpCharts.Controls.Add(cbEnableChannel, 1, chIdx);
}
截图。请注意,图表编号0和1位于splitContainer.Pannel1下方,并且列不会水平填充所有空格,但是它们会垂直填充。
修改 我将columnStyles移出了循环,并添加了背景颜色以查看发生了什么。这些是错误:
tableLayoutPanel顶部是表单而不是splitContainer.Panel2。请注意,第一个图表位于第一个面板后面。
private void Form1_Load(object sender,EventArgs e) {
tlpCharts.ColumnCount = 2;
tlpCharts.RowCount = _numChannels;
tlpCharts.Dock = DockStyle.Fill;
tlpCharts.BackColor = Color.Blue;
tlpCharts.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlpCharts.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
for (int chIdx = 0; chIdx < _numChannels; chIdx++)
{
tlpCharts.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
var cbEnableChannel = new CheckBox();
// customize chEnableChannel
var tmpChart = new Chart();
//customize tmpChart
tmpChart.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left);
// Add chart and checkbox
tlpCharts.Controls.Add(cbEnableChannel, 0, chIdx);
tlpCharts.Controls.Add(tmpChart, 1, chIdx);
}
}
元素:
//
// splitContainer1
//
this.splitContainer1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.tlpTop);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.tlpCharts);
this.splitContainer1.Panel2.Margin = new System.Windows.Forms.Padding(2);
this.splitContainer1.Panel2.Padding = new System.Windows.Forms.Padding(2);
this.splitContainer1.Panel2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.splitContainer1.Size = new System.Drawing.Size(784, 561);
this.splitContainer1.SplitterDistance = 72;
this.splitContainer1.TabIndex = 0;
//
// tlpTop
//
this.tlpTop.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tlpTop.ColumnCount = 4;
this.tlpTop.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tlpTop.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tlpTop.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tlpTop.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tlpTop.Controls.Add(this.grpOptions, 2, 0);
this.tlpTop.Controls.Add(this.btnPlay, 0, 0);
this.tlpTop.Controls.Add(this.grpDisplay, 1, 0);
this.tlpTop.Location = new System.Drawing.Point(3, 3);
this.tlpTop.Margin = new System.Windows.Forms.Padding(10);
this.tlpTop.Name = "tlpTop";
this.tlpTop.Padding = new System.Windows.Forms.Padding(0, 2, 0, 2);
this.tlpTop.RowCount = 1;
this.tlpTop.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tlpTop.Size = new System.Drawing.Size(781, 71);
this.tlpTop.TabIndex = 0;
//
// tlpCharts
//
this.tlpCharts.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tlpCharts.ColumnCount = 1;
this.tlpCharts.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tlpCharts.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 66.66666F));
this.tlpCharts.Location = new System.Drawing.Point(2, 5);
this.tlpCharts.Name = "tlpCharts";
this.tlpCharts.RowCount = 1;
this.tlpCharts.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 21.16183F));
this.tlpCharts.Size = new System.Drawing.Size(780, 478);
this.tlpCharts.TabIndex = 0;