Anchor属性

Control.Anchor 属性 :获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。

AnchorStyles 值的按位组合。默认值是 TopLeft

使用 Anchor 属性可以定义在调整控件的父控件大小时如何自动调整控件的大小。将控件锚定到其父控件后,可确保当调整父控件的大小时锚定的边缘与父控件的边缘的相对位置保持不变。

一个控件可以锚定到其容器的一个或多个边缘。例如,如果有一个带有 ButtonForm,而该按钮的 Anchor 属性值设置为 TopBottom,当 FormHeight 增加时,Button 伸展,以保持到 Form 的上边缘和下边缘的锚定距离不变。

下面的代码示例将 Button 添加到窗体并设置该控件的一些通用属性。示例将该按钮定位在窗体的右下角,因此调整窗体大小时其相对位置不变。接着,它设置 BackgroundImage 并将该按钮的大小调整为与 同样的大小。然后,该示例将 TabStop 设置为 true,并设置 TabIndex 属性。最后,它添加一个事件处理程序以处理按钮的 Click 事件。此示例要求您有一个名为 imageList1ImageList

// 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);
}

转载于:https://www.cnblogs.com/irving314/archive/2011/06/23/anchorstyle.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值