Condividi tramite


Control.Anchor Proprietà

Definizione

Ottiene o imposta i bordi del contenitore a cui è associato un controllo e determina il modo in cui un controllo viene ridimensionato con il relativo elemento padre.

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

Valore della proprietà

Combinazione bit per bit dei AnchorStyles valori. Il valore predefinito è Top e Left.

Esempio

Nell'esempio di codice seguente viene aggiunto un oggetto Button a una maschera e vengono impostate alcune delle relative proprietà comuni. Nell'esempio il pulsante viene ancorato all'angolo inferiore destro del form in modo da mantenere la relativa posizione relativa man mano che viene ridimensionata la maschera. Imposta quindi e BackgroundImage ridimensiona il pulsante con le stesse dimensioni di Image. Nell'esempio viene quindi impostato su TabStoptrue e viene impostata la TabIndex proprietà . Infine, aggiunge un gestore eventi per gestire l'evento Click del pulsante. Questo esempio richiede che sia presente un ImageList oggetto denominato 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

Commenti

Utilizzare la proprietà per definire il Anchor modo in cui un controllo viene ridimensionato automaticamente man mano che viene ridimensionato il controllo padre. L'ancoraggio di un controllo al controllo padre garantisce che i bordi ancorati rimangano nella stessa posizione rispetto ai bordi del controllo padre quando il controllo padre viene ridimensionato.

È possibile ancorare un controllo a uno o più bordi del relativo contenitore. Ad esempio, se si dispone di un Form oggetto con un il Button cui Anchor valore della proprietà è impostato su Top e Bottom, l'oggetto Button viene esteso per mantenere la distanza ancorata ai bordi superiore e inferiore di FormHeight quando viene aumentato .Form

Annotazioni

Le Anchor proprietà e Dock si escludono a vicenda. È possibile impostare un solo set alla volta e l'ultimo set ha la precedenza.

Note per gli eredi

Quando si esegue l'override della Anchor proprietà in una classe derivata, utilizzare la proprietà della classe di base per estendere l'implementazione di Anchor base. In caso contrario, è necessario fornire tutte le implementazioni. Non è necessario eseguire l'override di entrambe le get funzioni di accesso e set della Anchor proprietà. Se necessario, è possibile eseguirne l'override.

Si applica a

Vedi anche