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 軸(深度)。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.

適用於

另請參閱