Control.Anchor プロパティとは? わかりやすく解説

Control.Anchor プロパティ

コントロールバインドされるコンテナの端を取得または設定し、親のサイズ変更時にコントロールサイズどのように変化するかを決定します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

<LocalizableAttribute(True)> _
Public Overridable Property
 Anchor As AnchorStyles
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)

プロパティ
AnchorStyles 値のビットごとの組み合わせ既定値Top および Left です。

解説解説

コントロールサイズ変更合わせてコントロールサイズ自動的に変更する方法定義するには、Anchor プロパティ使用しますコントロールをその親コントロール固定すると、親コントロールサイズ変更されても、固定された端の位置を親コントロールの端に対して相対的に同じ位置に保つことができます

コントロールは、コンテナ1 つ上の端に固定できます。たとえば、Anchor プロパティ値が Top および Bottom設定されButton を持つ Form がある場合ButtonFormHeight大きくなるにつれて拡大されForm の上端と下端までの固定距離を維持します。

メモメモ

Anchor プロパティDock プロパティ同時に指定できません。いずれか一方のみを設定でき、一度両方プロパティ設定した場合は、最後に設定された方が優先されます。

継承時の注意 派生クラスAnchor プロパティオーバーライドする場合は、基本クラスAnchor プロパティ使用して基本実装拡張します。それ以外場合は、すべての実装提供する必要がありますAnchor プロパティget アクセサset アクセサ両方オーバーライドする必要はありません。必要に応じて 1 つアクセサだけをオーバーライドできます

使用例使用例

フォームButton追加し、共通プロパティ一部設定するコード例次に示します。この例では、ボタンフォーム右下隅に固定することにより、フォームサイズ変更されても、相対的な位置関係維持されるようにしています。次に、BackgroundImage を設定してボタンImage と同じサイズ変更します。この例では、TabStop を true設定しTabIndex プロパティ設定してます。最後にボタンClick イベント処理するイベント ハンドラ追加します。この例では、imageList1 という名前の ImageList存在している必要があります

' 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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Control.Anchor プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Control.Anchor プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS