Freigeben über


Control.Anchor-Eigenschaft

Ruft die Ränder des Containers ab, an die ein Steuerelement gebunden ist, oder legt diese fest und bestimmt, wie die Größe des Steuerelements mit dessen übergeordnetem Element geändert wird.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
<LocalizableAttribute(True)> _
Public Overridable Property Anchor As AnchorStyles
'Usage
Dim instance As Control
Dim value As AnchorStyles

value = instance.Anchor

instance.Anchor = value
[LocalizableAttribute(true)] 
public virtual AnchorStyles Anchor { get; set; }
[LocalizableAttribute(true)] 
public:
virtual property AnchorStyles Anchor {
    AnchorStyles get ();
    void set (AnchorStyles value);
}
/** @property */
public AnchorStyles get_Anchor ()

/** @property */
public void set_Anchor (AnchorStyles value)
public function get Anchor () : AnchorStyles

public function set Anchor (value : AnchorStyles)

Eigenschaftenwert

Eine bitweise Kombination der AnchorStyles-Werte. Der Standardwert ist Top und Left.

Hinweise

Definieren Sie mit der Anchor-Eigenschaft, wie die Größe eines Steuerelements automatisch geändert wird, sobald die Größe seines übergeordneten Steuerelements geändert wird. Das Verankern eines Steuerelements an seinem übergeordneten Steuerelement stellt sicher, dass bei einer Größenänderung des übergeordneten Steuerelements die verankerten Ränder relativ zu den Rändern des übergeordneten Steuerelements in derselben Position bleiben.

Ein Steuerelement können Sie an einem oder mehreren Rändern des Containers verankern. So wird z. B. bei einem Form mit einem Button, dessen Anchor-Eigenschaftenwert auf Top und Bottom festgelegt ist, der Button gestreckt, um den verankerten Abstand zum oberen und unteren Rand des Form beizubehalten, wenn die Height des Form vergrößert wird.

Hinweis

Die Anchor-Eigenschaft und die Dock-Eigenschaft schließen einander gegenseitig aus. Zum gleichen Zeitpunkt kann jeweils nur eine festgelegt werden, und die letzte Gruppe hat Vorrang.

Hinweise für Erben Verwenden Sie beim Überschreiben der Anchor-Eigenschaft in einer abgeleiteten Klasse die Anchor-Eigenschaft der Basisklasse, um die Basisimplementierung zu erweitern. Andernfalls müssen Sie die gesamte Implementierung bereitstellen. Sie müssen nicht sowohl den get-Accessor als auch den set-Accessor der Anchor-Eigenschaft überschreiben. Sie können ggf. auch nur einen Accessor überschreiben.

Beispiel

Im folgenden Codebeispiel wird einem Formular ein Button hinzugefügt, und es werden einige allgemeine Eigenschaften festgelegt. Im Beispiel wird die Schaltfläche in der rechten unteren Ecke des Formulars verankert, sodass sie auch dann ihre relative Position beibehält, wenn die Größe des Formulars geändert wird. Nach dem Festlegen des BackgroundImage wird die Größe der Schaltfläche auf die gleiche Größe wie das Image angepasst. Im Beispiel wird dann der TabStop auf true festgelegt. Außerdem wird die TabIndex-Eigenschaft festgelegt. Abschließend wird ein Ereignishandler hinzugefügt, um das Click-Ereignis der Schaltfläche zu behandeln. In diesem Beispiel muss ein ImageList mit der Bezeichnung imageList1 vorhanden sein.

' 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
// 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:
   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.set_Anchor(AnchorStyles.Bottom | AnchorStyles.Right);
    // Assign a background image.
    button1.set_BackgroundImage(imageList1.get_Images().get_Item(0));
    // Specify the layout style of the background image. Tile is the 
    // default.
    button1.set_BackgroundImageLayout(ImageLayout.Center);
    // Make the button the same size as the image.
    button1.set_Size(button1.get_BackgroundImage().get_Size());
    // Set the button's TabIndex and TabStop properties.
    button1.set_TabIndex(1);
    button1.set_TabStop(true);
    // Add a delegate to handle the Click event.
    button1.add_Click(new System.EventHandler(this.button1_Click));
    // Add the button to the form.
    this.get_Controls().Add(button1);
} //AddMyButton

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

Control-Klasse
Control-Member
System.Windows.Forms-Namespace
AnchorStyles-Enumeration
Dock
Layout