Share via


Control.Anchor Properti

Definisi

Mendapatkan atau mengatur tepi kontainer tempat kontrol terikat dan menentukan bagaimana kontrol diubah ukurannya dengan induknya.

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

Nilai Properti

Kombinasi bitwise dari AnchorStyles nilai. Defaultnya adalah Top dan Left.

Contoh

Contoh kode berikut menambahkan Button ke formulir dan mengatur beberapa properti umumnya. Contoh jangkar tombol ke sudut kanan bawah formulir sehingga mempertahankan posisi relatifnya saat formulir diubah ukurannya. Selanjutnya mengatur BackgroundImage dan mengubah ukuran tombol ke ukuran yang sama dengan Image. Contoh kemudian mengatur ke TabStoptrue dan mengatur TabIndex properti . Terakhir, ini menambahkan penanganan aktivitas untuk menangani Click peristiwa tombol. Contoh ini mengharuskan Anda memiliki nama ImageListimageList1.

   // 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

Keterangan

Anchor Gunakan properti untuk menentukan bagaimana kontrol diubah ukurannya secara otomatis karena kontrol induknya diubah ukurannya. Penahanan kontrol ke kontrol induknya memastikan bahwa tepi yang berlabuh tetap berada dalam posisi yang sama relatif terhadap tepi kontrol induk saat kontrol induk diubah ukurannya.

Anda dapat membuat jangkar kontrol ke satu atau beberapa tepi kontainernya. Misalnya, jika Anda memiliki Form dengan Button nilai properti yang Anchor diatur ke Top dan Bottom, Button direntangkan untuk mempertahankan jarak berlabuh ke tepi FormHeight atas dan bawah saat dari Form ditingkatkan.

Catatan

Properti Anchor dan Dock saling eksklusif. Hanya satu yang dapat diatur pada satu waktu, dan satu set terakhir lebih diutamakan.

Catatan Bagi Inheritor

Saat mengambil alih Anchor properti di kelas turunan, gunakan properti kelas Anchor dasar untuk memperluas implementasi dasar. Jika tidak, Anda harus menyediakan semua implementasi. Anda tidak diharuskan untuk mengambil alih aksesor getAnchor dan set properti; Anda hanya dapat mengambil alih satu jika diperlukan.

Berlaku untuk

Lihat juga