Control.Dock 属性

定义

获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。

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

DockStyle 值之一。 默认值为 None

例外

分配的值不是 DockStyle 值之一。

示例

下面的代码示例创建 GroupBox 并设置其一些常见属性。 该示例在 TextBox 组框中创建并设置其 Location 。 接下来,它将组框的属性设置为 Text 组框,并将组框停靠在窗体顶部。 最后,它将属性false设置为Enabled禁用组框,这将导致禁用组框中包含的所有控件。

   // 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 属性定义控件在调整父控件大小时自动调整其大小的方式。 例如,设置为DockDockStyle.Left使控件与父控件的左边缘对齐,并在调整父控件大小时调整大小。 控件以 Z 顺序停靠,它是窗体上控件的视觉分层,沿窗体的 Z 轴 (深度) 。

控件可以停靠到其父容器的一个边缘,也可以停靠到所有边缘并填充父容器。

Margin在停靠控件上设置属性不会影响控件与其容器边缘之间的距离。

备注

属性AnchorDock是互斥的。 一次只能设置一个,最后一个集优先。

继承者说明

重写 Dock 派生类中的属性时,请使用基类的属性 Dock 扩展基实现。 否则,必须提供所有实现。 无需重写 get 属性和 set 方法 Dock ;仅在需要时可以替代一个。

适用于

另请参阅