共用方式為


Control.Anchor 屬性

定義

取得或設定控制項綁定容器的邊緣,並決定控制項如何與父控制項進行調整大小。

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 。 預設為 TopLeft

範例

以下程式碼範例將 a Button 加入表單,並設定其一些常見屬性。 範例中將按鈕固定在表單的右下角,以保持表單調整大小時的相對位置。 接著它設定 , BackgroundImage 並將按鈕調整到和 Image. 相同的大小。 範例接著設定 到 TabStoptrue設定屬性。TabIndex 最後,它新增了一個事件處理程序來處理 Click 按鈕事件。 此範例要求你有一個 ImageList 名為 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 屬性來定義如何隨著父控制項的調整而自動調整大小。 將控制項錨定到其父控制項,確保在父控制項重新調整大小時,錨定的邊相對於父控制項的邊緣保持不變位置。

你可以將控制項錨定在其容器的一個或多個邊緣上。 例如,若 具有 Form 屬性ButtonAnchor值為 TopBottom且 ,Button則 會被拉伸以維持與 Form 的錨定距離,隨著 HeightForm 的增加。

備註

AnchorDock 屬性互斥。 一次只能設定一個,最後一個集合優先。

給繼承者的注意事項

在導出類別中覆寫該 Anchor 屬性時,請使用基底類別的 Anchor 特性來擴充基底實作。 否則,你必須提供所有的實作。 你不必同時覆寫get該財產的setAnchor附屬裝置;如果需要,你只能覆蓋其中一項。

適用於

另請參閱