Share via


Control.Dock 属性

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Overridable Property Dock As DockStyle
用法
Dim instance As Control
Dim value As DockStyle

value = instance.Dock

instance.Dock = value
[LocalizableAttribute(true)] 
public virtual DockStyle Dock { get; set; }
[LocalizableAttribute(true)] 
public:
virtual property DockStyle Dock {
    DockStyle get ();
    void set (DockStyle value);
}
/** @property */
public DockStyle get_Dock ()

/** @property */
public void set_Dock (DockStyle value)
public function get Dock () : DockStyle

public function set Dock (value : DockStyle)

属性值

DockStyle 值之一。默认为 None

异常

异常类型 条件

InvalidEnumArgumentException

分配的值不是 DockStyle 值之一。

备注

使用 Dock 属性可以定义在调整控件的父控件大小时如何自动调整控件的大小。例如,将 Dock 设置为 DockStyle.Left 将导致控件与其父控件的左边缘对齐,并在父控件调整大小时调整自身大小。控件按照 Z 顺序停靠,Z 顺序是窗体上的控件沿窗体的 Z 轴(深度)的可视化分层。

一个控件可以停靠到其父容器的一个边缘或者可以停靠到所有边缘并充满父容器。

提示

AnchorDock 属性是互相排斥的。每次只可以设置一个属性,最后设置的属性优先。

给继承者的说明 在派生类中重写 Dock 属性时,请使用基类的 Dock 属性来扩展基实现。否则,您必须提供所有实现。不需要同时重写 Dock 属性的 getset 方法;如果需要,可以只重写其中一个方法。

示例

下面的代码示例创建 GroupBox 并设置该控件的一些通用属性。该示例创建一个 TextBox 并设置它在分组框中的 Location。接着,它设置分组框的 Text 属性,并将此分组框停靠在窗体的顶部。最后,它通过将 Enabled 属性设置为 false 来禁用分组框,这将导致分组框中的所有控件都被禁用。

' 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
// 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:
   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.set_Location(new Point(15, 15));
    groupBox1.get_Controls().Add(textBox1);
    // Set the Text and Dock properties of the GroupBox.
    groupBox1.set_Text("MyGroupBox");
    groupBox1.set_Dock(DockStyle.Top);
    // Disable the GroupBox (which disables all its child controls)
    groupBox1.set_Enabled(false);
    // Add the Groupbox to the form.
    this.get_Controls().Add(groupBox1);
} //AddMyGroupBox

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
DockStyle
Control.Anchor 属性
ScrollableControl.DockPaddingEdges
Layout