Control.Dock 属性

定义

获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。Gets or sets which control borders are docked to its parent control and determines how a control is resized with its parent.

public:
 virtual property System::Windows::Forms::DockStyle Dock { System::Windows::Forms::DockStyle get(); void set(System::Windows::Forms::DockStyle value); };
public virtual System.Windows.Forms.DockStyle Dock { get; set; }
member this.Dock : System.Windows.Forms.DockStyle with get, set
Public Overridable Property Dock As DockStyle

属性值

DockStyle 值之一。One of the DockStyle values. 默认值为 NoneThe default is None.

异常

分配的值不是 DockStyle 值之一。The value assigned is not one of the DockStyle values.

示例

下面的代码示例创建 GroupBox,并设置其一些公共属性。The following code example creates a GroupBox and sets some of its common properties. 该示例创建一个 TextBox 并在 "分组" 框中设置其 LocationThe example creates a TextBox and sets its Location within the group box. 接下来,它设置分组框的 Text 属性,并将组框停靠到窗体的顶部。Next, it sets the Text property of the group box, and docks the group box to the top of the form. 最后,它通过将 Enabled 属性设置为 false 来禁用分组框,这将导致禁用组中包含的所有控件。Lastly, it disables the group box by setting the Enabled property to false, which causes all controls contained within the group box to be disabled.

   // 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 = gcnew GroupBox;
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Location = 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 );
   }
// 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);
}
' Add a GroupBox to a form and set some of its common properties.
Private Sub AddMyGroupBox()
   ' Create a GroupBox and add a TextBox to it.
   Dim groupBox1 As New GroupBox()
   Dim textBox1 As 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.
   Me.Controls.Add(groupBox1)
End Sub

注解

使用 Dock 属性可定义控件在其父控件调整大小时如何自动调整大小。Use the Dock property to define how a control is automatically resized as its parent control is resized. 例如,将 Dock 设置为 DockStyle.Left 会导致控件与其父控件的左边缘对齐,并在父控件调整大小时进行重设大小。For example, setting Dock to DockStyle.Left causes the control to align itself with the left edges of its parent control and to resize as the parent control is resized. 控件按其 Z 顺序停靠,这是窗体上的控件沿窗体的 Z 轴(深度)的可视化分层。Controls are docked in their Z-order, which is the visual layering of controls on a form along the form's Z-axis (depth).

控件可以停靠到其父容器的一个边缘,也可以停靠到所有边缘并填充父容器。A control can be docked to one edge of its parent container or can be docked to all edges and fill the parent container.

设置停靠控件的 Margin 属性不会影响控件距其容器边缘的距离。Setting the Margin property on a docked control has no effect on the distance of the control from the edges of its container.

备注

AnchorDock 属性互相排斥。The Anchor and Dock properties are mutually exclusive. 一次只能设置一个,而最后一个集则优先。Only one can be set at a time, and the last one set takes precedence.

继承者说明

在派生类中重写 Dock 属性时,请使用基类的 Dock 属性来扩展基实现。When overriding the Dock property in a derived class, use the base class's Dock property to extend the base implementation. 否则,必须提供所有实现。Otherwise, you must provide all the implementation. 不需要重写 Dock 属性的 getset 方法;如果需要,只能重写一个。You are not required to override both the get and set methods of the Dock property; you can override only one if needed.

适用于

另请参阅