使用DOCK属性可以获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。
使用 Dock 属性可以定义在调整控件的父控件大小时如何自动调整控件的大小。例如,将 Dock 设置为 DockStyle.Left 将导致控件与其父控件的左边缘对齐,并在父控件调整大小时调整自身大小。控件按照 Z 顺序停靠,Z 顺序是窗体上的控件沿窗体的 Z 轴(深度)的可视化分层。
一个控件可以停靠到其父容器的一个边缘或者可以停靠到所有边缘并充满父容器。
// Add a GroupBox to a form and set some of its common properties.
private void AddMyGroupBox()
{
// Create a GroupBox and add a TextBox to it.
GroupBox groupBox1 = new GroupBox();
TextBox textBox1 = new TextBox();
textBox1.Location = new Point(15, 15);
groupBox1.Controls.Add(textBox1);
// Set the Text and Dock properties of the GroupBox.
groupBox1.Text = "MyGroupBox";
groupBox1.Dock = DockStyle.Top;
// Disable the GroupBox (which disables all its child controls)
groupBox1.Enabled = false;
// Add the Groupbox to the form.
this.Controls.Add(groupBox1);
}