注意:此类在 .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
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
public ref class TableLayoutPanel : public Panel, IExtenderProvider
/** @attribute ComVisibleAttribute(true) */
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
public class TableLayoutPanel extends Panel implements IExtenderProvider
ComVisibleAttribute(true)
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)
public class TableLayoutPanel extends Panel implements IExtenderProvider
TableLayoutPanel 控件在网格中排列其内容。因为布局既可以在设计时执行,也可以在运行时执行,所以它会随应用程序环境的更改而动态更改。这样,面板中的控件可以适当地调整大小,从而对各种更改做出响应,如父控件的大小调整或者由于本地化带来的文本长度的更改。
任何 Windows 窗体控件都可以是 TableLayoutPanel 控件的子级,包括 TableLayoutPanel 的其他实例。这样,您就可以构造能够适应运行时的各种更改的复杂布局。
TableLayoutPanel 控件可以根据 RowCount、ColumnCount 和 GrowStyle 属性的值进行扩展,以容纳新添加的控件。将 RowCount 或 ColumnCount 属性的值设置为 0,将指定 TableLayoutPanel 在相应方向上取消绑定。
当 TableLayoutPanel 控件充满子控件以后,您也可以控制扩展的方向(水平或垂直)。默认情况下,TableLayoutPanel 控件通过添加行向下扩展。
如果希望行和列采取与默认行为不同的行为方式,可以通过使用 RowStyles 和 ColumnStyles 属性来控制行和列的属性。可以分别设置行或列的属性。
TableLayoutPanel 控件向其子控件添加以下属性:Cell、Column、Row、ColumnSpan 和 RowSpan。
可以通过设置子控件上的 ColumnSpan 或 RowSpan 属性,来合并 TableLayoutPanel 控件中的单元格。
子控件的停靠行为与其他容器控件相同。
TableLayoutPanel 中子控件的锚定行为与其他容器控件的行为不同。如果将子控件的 Anchor 属性值设置为 Left 或 Right,则将控件靠单元格左边框或由边框放置,距离边框的距离是控件的 Margin 属性和面板的 Padding 属性之和。如果同时设置了 Left 和 Right 值,控件将根据单元格的宽度调整大小,同时考虑了 Margin 和 Padding 值。Top 和 Bottom 定锚的行为与此类似。有关更多信息,请参见 如何:在 TableLayoutPanel 控件中锚定和停靠子控件。
如果需要子控件模仿其他容器控件的默认定锚行为,可以调整 Margin 和 Padding 属性,以使控件的边框与单元格的边框保持固定的距离。
运行时,将一个子控件的 Column 和 Row 属性值设置为 -1,将使该控件移动到 TableLayoutPanel 控件的第一个空单元格中。空单元格将按从左到右、从上到下的搜索顺序选中。此顺序依赖于区域性,所以在从右向左 (RTL) 的布局中它将正确运行。设计时,将这些属性设置为 -1 时,子控件不会移动。
下面的代码示例演示了如何重写 OnCellPaint 方法以创建单元格的自定义外观。有关使用 TableLayoutPanel 控件来创建很好地响应大小调整的布局的示例,请参见 TableLayoutPanel 控件示例。
Public Class DemoTableLayoutPanel
Inherits TableLayoutPanel
Protected Overrides Sub OnCellPaint( _
ByVal e As System.Windows.Forms.TableLayoutCellPaintEventArgs)
MyBase.OnCellPaint(e)
Dim c As Control = Me.GetControlFromPosition(e.Column, e.Row)
If Not c Is Nothing Then
Dim g As Graphics = e.Graphics
g.DrawRectangle( _
Pens.Red, _
e.CellBounds.Location.X + 1, _
e.CellBounds.Location.Y + 1, _
e.CellBounds.Width - 2, _
e.CellBounds.Height - 2)
g.FillRectangle( _
Brushes.Blue, _
e.CellBounds.Location.X + 1, _
e.CellBounds.Location.Y + 1, _
e.CellBounds.Width - 2, _
e.CellBounds.Height - 2)
End If
End Sub
End Class
public class DemoTableLayoutPanel : TableLayoutPanel
{
protected override void OnCellPaint(TableLayoutCellPaintEventArgs e)
{
base.OnCellPaint(e);
Control c = this.GetControlFromPosition(e.Column, e.Row);
if ( c != null )
{
Graphics g = e.Graphics;
g.DrawRectangle(
Pens.Red,
e.CellBounds.Location.X+1,
e.CellBounds.Location.Y + 1,
e.CellBounds.Width - 2, e.CellBounds.Height - 2);
g.FillRectangle(
Brushes.Blue,
e.CellBounds.Location.X + 1,
e.CellBounds.Location.Y + 1,
e.CellBounds.Width - 2,
e.CellBounds.Height - 2);
};
}
}
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.Panel
System.Windows.Forms.TableLayoutPanel
System.ComponentModel.Design.ByteViewer
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0