注意:此类在 .NET Framework 2.0 版中是新增的。
表示一个面板,它可以在一个由行和列组成的网格中对其内容进行动态布局。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class TableLayoutPanel Inherits Panel Implements IExtenderProvider
Dim instance As TableLayoutPanel
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class TableLayoutPanel : Panel, IExtenderProvider
TableLayoutPanel 控件的最佳做法
TableLayoutPanel 控件提供了强大的布局功能,在 Windows 窗体上使用该控件之前应仔细考虑这些功能。
建议
下面的建议将帮助您最有效地使用 TableLayoutPanel 控件。
目标用法
请有选择地使用 TableLayoutPanel 控件。不应在一切需要可调整大小的布局的情况下都使用该控件。下面的列表描述了能从 TableLayoutPanel 控件的使用中最大程度获益的布局:
-
在其中窗体的多个部件成比例调整大小的布局。
-
将在运行时动态修改或生成的布局,如根据首选项增减了用户自定义字段的数据输入窗体。
-
应该保持在总体固定大小的布局。例如,您可能有一个应保持小于 800 x 600 的对话框,但是您需要支持本地化的字符串。
下面的列表描述了不会从 TableLayoutPanel 控件的使用中获得很大益处的布局:
-
只有一个标签列和一个文本输入区域列的简单数据输入窗体。
-
含有单个较大显示区域的窗体,该显示区域应在调整大小时填充所有可用的空间。此类窗体的一个示例是显示单个 PropertyGrid 控件的窗体。在此情况下,请使用锚定,因为在调整窗体大小时不应展开任何其他项。
请不要停靠填充
不要将 TableLayoutPanel 控件的 Dock 属性设置为 Fill 整个窗体。大多数情况下,您希望 TableLayoutPanel 控件所支持的按比例调整大小的行为能影响窗体上的一组特定控件。当 TableLayoutPanel 控件填充整个窗体时,极少有方案能从中获益。
避免嵌套
避免在一个 TableLayoutPanel 控件中嵌套其他 TableLayoutPanel 控件。顶层 TableLayoutPanel 控件的性能相对较好,但是它被设计为最多只能处理一层嵌套。更深层的嵌套可能会导致在调整窗体大小时性能受损。
避免可视化继承
避免可视化继承。尽量不要将 TableLayoutPanel 控件放入基窗体中,也不要在派生类中添加更多控件。这样做会创建由设计器生成的有可能降低性能的代码,并且可能由此引发其他问题。
请参见
【转】
c# winForm TableLayoutPanel学习 合并行的布局及动态增加删除内部控件
先通过在IDE中拉出这样的效果图 以学习相关属性
上面效果的相关代码
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.button1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.button2, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.button3, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.button4, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.button5, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.button6, 1, 2);
this.tableLayoutPanel1.Location = new System.Drawing.Point(46, 35);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 5;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(200, 195);
this.tableLayoutPanel1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(4, 4);
this.button1.Name = "button1";
this.tableLayoutPanel1.SetRowSpan(this.button1, 2);
this.button1.Size = new System.Drawing.Size(75, 79);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(103, 4);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.None;
this.button3.Location = new System.Drawing.Point(112, 53);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
//
// button4
//
this.button4.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.button4.Location = new System.Drawing.Point(4, 90);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(92, 36);
this.button4.TabIndex = 3;
this.button4.Text = "button4";
this.button4.UseVisualStyleBackColor = true;
//
// button5
//
this.button5.Anchor = System.Windows.Forms.AnchorStyles.None;
this.button5.Location = new System.Drawing.Point(12, 139);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 4;
this.button5.Text = "button5";
this.button5.UseVisualStyleBackColor = true;
//
// button6
//
this.button6.Location = new System.Drawing.Point(103, 90);
this.button6.Name = "button6";
this.tableLayoutPanel1.SetRowSpan(this.button6, 2);
this.button6.Size = new System.Drawing.Size(75, 79);
this.button6.TabIndex = 5;
this.button6.Text = "button6";
this.button6.UseVisualStyleBackColor = true;
//
// tableLayoutPanelForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 265);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "tableLayoutPanelForm";
this.Text = "tableLayoutPanelForm";
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
在上面属性学习的基础上 通过编码来创建我们需要的TableLayoutPanel的相关布局
下面想要实现的效果为:
点击一次按钮 TableLayoutPanel将新增两行
此两行的相关控件布局如下
button1 button2
button3
再点击一次按钮 将又会新增两行
button4 button5
button6
其中我们也将对按钮加入相关事件
点击button1 得到自身的Name值
点击button2 得到其对应的button1的Text值 (button5对应于button4)
点击button3 将删除自身及其对应的button1 和 button2(点击button6将删除button6 button5 button4)
相关示例代码如下
public partial class tableLayoutPanelForm : Form
{
public tableLayoutPanelForm()
{
InitializeComponent();
}
private void tableLayoutPanelForm_Load(object sender, EventArgs e)
{
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = BorderStyle.FixedSingle;
TableLayoutPanel tabPanel = new TableLayoutPanel();
tabPanel.ColumnCount = 2;
this.panel1.Controls.Add(tabPanel);
}
int iClickedTimes = 0;
//每点击一次 新增如下结构
//btn1 btn2
// btn3
//btn1跨两行
private void button1_Click(object sender, EventArgs e)
{
TableLayoutPanel tabPanel = (TableLayoutPanel)(this.panel1.Controls[0]);
tabPanel.RowCount = (iClickedTimes + 1) * 2; //再增两行
//增加button1
Button btn1 = new Button();
btn1.Name = "button" + (iClickedTimes * 3 + 1).ToString();
btn1.Text = "buttonText"+ (iClickedTimes*3+1).ToString();
btn1.Height = btn1.Height * 3;
btn1.Click += new EventHandler(btn1Click);//事件
tabPanel.Controls.Add(btn1, 0, iClickedTimes*2);
tabPanel.SetRowSpan(btn1, 2);//跨两行
//增加button2
Button btn2 = new Button();
btn2.Name = "button" + (iClickedTimes * 3 + 2).ToString();
btn2.Text = "buttonText" + (iClickedTimes * 3 + 2).ToString();
btn2.Click += new EventHandler(btn2Click);
tabPanel.Controls.Add(btn2, 1, iClickedTimes * 2);
//增加button3
Button btn3 = new Button();
btn3.Name = "button" + (iClickedTimes * 3 + 3).ToString();
btn3.Text = "buttonText" + (iClickedTimes * 3 + 3).ToString();
btn3.Click += new EventHandler(btn3Click);
tabPanel.Controls.Add(btn3, 1, iClickedTimes * 2 + 1);
tabPanel.Size=new Size(200, (iClickedTimes + 1) * 2 * 50);
iClickedTimes++;
}
//自身的Name
private void btn1Click(object sender, EventArgs e)
{
Button btn1 = (Button)sender;
MessageBox.Show(btn1.Name);
}
//通过命名规则 得到其对应btn1的Text
private void btn2Click(object sender, EventArgs e)
{
Button btn2 = (Button)sender;
string btn2ID = btn2.Name.Substring(6);//去掉Name中的button
//其对应btn1的Name为
string btn1Name = "button" + Convert.ToString((Convert.ToInt32(btn2ID) - 1));
//
TableLayoutPanel tabPanel = (TableLayoutPanel)(this.panel1.Controls[0]);
string btn1Text = ((Button)(tabPanel.Controls[btn1Name])).Text;
MessageBox.Show(btn1Text);
}
//删除其所在行列 btn1,btn2,btn3
private void btn3Click(object sender, EventArgs e)
{
Button btn3 = (Button)sender;
TableLayoutPanel tabPanel = (TableLayoutPanel)(this.panel1.Controls[0]);
//一种删除方法 得到其对应btn1 btn2的Name
string btn3ID = btn3.Name.Substring(6);
string btn1Name = "button" + Convert.ToString((Convert.ToInt32(btn3ID) - 2));
string btn2Name = "button" + Convert.ToString((Convert.ToInt32(btn3ID) - 1));
tabPanel.Controls.Remove(btn3);
tabPanel.Controls.Remove(tabPanel.Controls[btn2Name]);
tabPanel.Controls.Remove(tabPanel.Controls[btn1Name]);
}
}