Control.Dock Control.Dock Control.Dock Control.Dock Property

定义

获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。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.

异常

示例

下面的代码示例创建GroupBox和设置的某些常见属性。The following code example creates a GroupBox and sets some of its common properties. 此示例将创建TextBox,并设置其Location组框中。The 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. 例如,设置DockDockStyle.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. 不需要重写二者getset方法的Dock属性; 您可以根据需要覆盖只有一个。You are not required to override both the get and set methods of the Dock property; you can override only one if needed.

适用于

另请参阅