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. 不需要重写get Dock属性的和set方法; 只有在需要时才可以重写。You are not required to override both the get and set methods of the Dock property; you can override only one if needed.

适用于

另请参阅