AnchorStyles Enum

Definition

Anger hur en kontroll fäster vid kanterna på containern.

Den här uppräkningen stöder en bitvis kombination av dess medlemsvärden.

public enum class AnchorStyles
[System.Flags]
public enum AnchorStyles
[<System.Flags>]
type AnchorStyles = 
Public Enum AnchorStyles
Arv
AnchorStyles
Attribut

Fält

Name Värde Description
None 0

Kontrollen är inte förankrad i någon kant av containern.

Top 1

Kontrollen är fäst vid containerns övre kant.

Bottom 2

Kontrollen är fäst vid containerns nedre kant.

Left 4

Kontrollen är fäst vid containerns vänstra kant.

Right 8

Kontrollen är fäst vid containerns högra kant.

Exempel

I följande exempel läggs ett Button till i ett formulär och några av dess gemensamma egenskaper anges. Exemplet fäster knappen i formulärets nedre högra hörn så att den behåller sin relativa position när formuläret ändras. Därefter ställer den BackgroundImage in och ändrar storlek på knappen till samma storlek som Image. Exemplet anger TabStop sedan egenskapen till true och TabIndex . Slutligen lägger den till en händelsehanterare för att hantera Click händelsen för knappen. Det här exemplet förutsätter att du har namnet 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

Kommentarer

När en kontroll är fäst vid en kant av containern förblir avståndet mellan kontrollen och den angivna gränsen konstant när containern ändrar storlek. Om en kontroll till exempel är fäst vid containerns högra kant förblir avståndet mellan kontrollens högra kant och containerns högra kant konstant när containern ändrar storlek. En kontroll kan förankras i valfri kombination av kontrollkanter. Om kontrollen är fäst vid motsatta kanter i containern (till exempel längst upp och ned) ändrar den storlek när containern ändrar storlek. Om en kontroll har sin Anchor egenskap inställd på Ingen flyttas hälften av avståndet som kontrollens container har ändrat storlek på. Om en Button till exempel har sin Anchor egenskap inställd på Ingen och Form kontrollen finns på ändras storlek med 20 bildpunkter i båda riktningarna, flyttas knappen 10 bildpunkter i båda riktningarna.

Gäller för

Se även