Control.Anchor Propriedade

Definição

Obtém ou define as bordas do contêiner ao qual um controle está vinculado e determina como um controle é redimensionado com seu pai.

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

Valor da propriedade

Uma combinação bit a bit dos valores AnchorStyles. O padrão é Top e Left.

Exemplos

O exemplo de código a seguir adiciona um a um Button formulário e define algumas de suas propriedades comuns. O exemplo ancora o botão no canto inferior direito do formulário para que ele mantenha sua posição relativa à medida que o formulário é redimensionado. Em seguida, ele define o BackgroundImage e redimensiona o botão para o mesmo tamanho que o Image. Em seguida, o exemplo define como TabStoptrue e define a TabIndex propriedade . Por fim, ele adiciona um manipulador de eventos para manipular o Click evento do botão. Este exemplo exige que você tenha um ImageList chamado 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

Comentários

Use a Anchor propriedade para definir como um controle é redimensionado automaticamente à medida que seu controle pai é redimensionado. Ancorar um controle em seu controle pai garante que as bordas ancoradas permaneçam na mesma posição em relação às bordas do controle pai quando o controle pai for redimensionado.

Você pode ancorar um controle em uma ou mais bordas de seu contêiner. Por exemplo, se você tiver um Form com um Button cujo Anchor valor de propriedade está definido Top como e Bottom, o Button será estendido para manter a distância ancorada nas bordas superior e inferior do Form conforme o HeightForm do é aumentado.

Observação

As Anchor propriedades e Dock são mutuamente exclusivas. Somente um pode ser definido por vez e o último conjunto tem precedência.

Notas aos Herdeiros

Ao substituir a Anchor propriedade em uma classe derivada, use a propriedade da Anchor classe base para estender a implementação base. Caso contrário, você deve fornecer toda a implementação. Você não precisa substituir os get acessadores e set da Anchor propriedade; você pode substituir apenas um, se necessário.

Aplica-se a

Confira também