Control.Anchor Control.Anchor Control.Anchor Control.Anchor Property

定义

获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.

public:
 virtual property System::Windows::Forms::AnchorStyles Anchor { System::Windows::Forms::AnchorStyles get(); void set(System::Windows::Forms::AnchorStyles value); };
public virtual System.Windows.Forms.AnchorStyles Anchor { get; set; }
member this.Anchor : System.Windows.Forms.AnchorStyles with get, set
Public Overridable Property Anchor As AnchorStyles

属性值

AnchorStyles 值的按位组合。A bitwise combination of the AnchorStyles values. 默认值是 TopLeftThe default is Top and Left.

示例

下面的代码示例添加Button向窗体,并设置它的一些常见属性。The following code example adds a Button to a form and sets some of its common properties. 示例定位到窗体右下角的按钮,以便在调整窗体大小时,它使其相对位置。The example anchors the button to the bottom-right corner of the form so it keeps its relative position as the form is resized. 接下来设置BackgroundImage调整大小的按钮为具有相同的大小和ImageNext it sets the BackgroundImage and resizes the button to the same size as the Image. 该示例然后设置TabStoptrue,并设置TabIndex属性。The example then sets the TabStop to true and sets the TabIndex property. 最后,它将添加一个事件处理程序以处理Click按钮的事件。Lastly, it adds an event handler to handle the Click event of the button. 此示例要求具有ImageList名为imageList1This example requires that you have an ImageList named imageList1.

   // Add a button to a form and set some of its common properties.
private:
   void AddMyButton()
   {
      // Create a button and add it to the form.
      Button^ button1 = gcnew Button;

      // Anchor the button to the bottom right corner of the form
      button1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Bottom | AnchorStyles::Right);

      // Assign a background image.
      button1->BackgroundImage = imageList1->Images[ 0 ];

      // Specify the layout style of the background image. Tile is the default.
      button1->BackgroundImageLayout = ImageLayout::Center;

      // Make the button the same size as the image.
      button1->Size = button1->BackgroundImage->Size;

      // Set the button's TabIndex and TabStop properties.
      button1->TabIndex = 1;
      button1->TabStop = true;

      // Add a delegate to handle the Click event.
      button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );

      // Add the button to the form.
      this->Controls->Add( button1 );
   }
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
   // Create a button and add it to the form.
   Button button1 = new Button();

   // Anchor the button to the bottom right corner of the form
   button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

   // Assign a background image.
   button1.BackgroundImage = imageList1.Images[0];

   // Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center;
   
   // Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size;

   // Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1;
   button1.TabStop = true;

   // Add a delegate to handle the Click event.
   button1.Click += new System.EventHandler(this.button1_Click);

   // Add the button to the form.
   this.Controls.Add(button1);
}
' Add a button to a form and set some of its common properties.
Private Sub AddMyButton()
   ' Create a button and add it to the form.
   Dim button1 As New Button()
   
   ' Anchor the button to the bottom right corner of the form
   button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
   
   ' Assign a background image.
   button1.BackgroundImage = imageList1.Images(0)

   ' Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center
   
   ' Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size
   
   ' Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1
   button1.TabStop = True

   ' Add a delegate to handle the Click event.
   AddHandler button1.Click, AddressOf Me.button1_Click
   
   ' Add the button to the form.
   Me.Controls.Add(button1)
End Sub

注解

使用Anchor属性来定义如何控件自动调整大小调整其父控件的大小时。Use the Anchor property to define how a control is automatically resized as its parent control is resized. 锚定到其父控件的控件可确保父控件在调整大小时,定位的边缘也保留在相对于边缘的父控件相同的位置。Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

您可以定位到其容器的一个或多个边缘的控件。You can anchor a control to one or more edges of its container. 例如,如果你有FormButtonAnchor属性值设置为TopBottom,则Button拉伸以维护到边缘和下边缘的锚定的距离Form作为HeightForm增加。For example, if you have a Form with a Button whose Anchor property value is set to Top and Bottom, the Button is stretched to maintain the anchored distance to the top and bottom edges of the Form as the Height of the Form is increased.

备注

AnchorDock属性是互相排斥。The Anchor and Dock properties are mutually exclusive. 只有一个可以设置一次和最后一个设置将优先。Only one can be set at a time, and the last one set takes precedence.

继承者说明

重写时Anchor属性在派生类中,使用基类的Anchor属性扩展的基实现。When overriding the Anchor property in a derived class, use the base class's Anchor property to extend the base implementation. 否则,必须提供所有的实现。Otherwise, you must provide all the implementation. 不需要重写二者getset访问器Anchor属性; 您可以根据需要覆盖只有一个。You are not required to override both the get and set accessors of the Anchor property; you can override only one if needed.

适用于

另请参阅