ScrollableControl.AutoScrollMargin 属性

获取或设置自动滚动边距的大小。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Property AutoScrollMargin As Size
用法
Dim instance As ScrollableControl
Dim value As Size

value = instance.AutoScrollMargin

instance.AutoScrollMargin = value
[LocalizableAttribute(true)] 
public Size AutoScrollMargin { get; set; }
[LocalizableAttribute(true)] 
public:
property Size AutoScrollMargin {
    Size get ();
    void set (Size value);
}
/** @property */
public Size get_AutoScrollMargin ()

/** @property */
public void set_AutoScrollMargin (Size value)
public function get AutoScrollMargin () : Size

public function set AutoScrollMargin (value : Size)

属性值

Size,表示自动滚动边距的高度和宽度(以像素为单位)。

异常

异常类型 条件

ArgumentOutOfRangeException

分配的 HeightWidth 值小于 0。

备注

自动滚动边距是任何子控件和可滚动父控件边界之间的距离。可滚动控件中包含的任何子控件的大小都应加上 AutoScrollMargin 的大小,以确定是否需要滚动条。重新调整父级可滚动控件的大小时,或将单个子控件放入视图时,将计算 AutoScrollMargin 属性,并用该属性确定是否必须显示滚动条。在确定是否必须显示滚动条的计算中,停靠控件应排除在外。

提示

如果某个停靠控件的 Dock 属性设置为 DockStyle.Fill,则该控件会填充到父级可滚动控件内;在使用 AutoScrollMargin 来确定是否需要滚动条时,将忽略该停靠控件。

如果从子控件的边缘到父级可滚动控件的距离小于赋予 AutoScrollMargin 属性的值,并且 AutoScroll 属性已设置为 true,则会显示相应的滚动条。

提示

建议在可滚动控件内停靠控件时添加一个子级可滚动控件(如 Panel),以包含可能需要滚动的任何其他控件。应将子级 Panel 控件添加到可滚动控件,将其 Dock 属性设置为 DockStyle.Fill,将其 AutoScroll 属性设置为 true。应将父级可滚动控件的 AutoScroll 属性设置为 false

示例

下面的代码示例使用派生类 Panel。该示例计算文本框的位置并更改其父容器(面板控件)的外观和行为。该示例要求已创建 Panel 控件、TextBoxButton 的实例。将文本框放在面板上,以便它至少与面板的一条边缘重叠。单击一个按钮调用此函数可查看面板行为和外观上的差异。

Private Sub SetAutoScrollMargins()
    ' If the text box is outside the panel's bounds,
    ' turn on auto-scrolling and set the margin. 
    If (text1.Location.X > panel1.Location.X) Or _
        (text1.Location.Y > panel1.Location.Y) Then
        panel1.AutoScroll = True
        ' If the AutoScrollMargin is set to less
        ' than (5,5), set it to 5,5. 
        If (panel1.AutoScrollMargin.Width < 5) Or _
            (panel1.AutoScrollMargin.Height < 5) Then
            
            panel1.SetAutoScrollMargin(5, 5)
        End If
    End If
End Sub
private void SetAutoScrollMargins()
 {
    /* If the text box is outside the panel's bounds, 
       turn on auto-scrolling and set the margin. */  
    if (text1.Location.X > panel1.Location.X || 
       text1.Location.Y > panel1.Location.Y)
    {
       panel1.AutoScroll = true;
       /* If the AutoScrollMargin is set to less 
          than (5,5), set it to 5,5. */
       if( panel1.AutoScrollMargin.Width < 5 || 
          panel1.AutoScrollMargin.Height < 5)
       {
          panel1.SetAutoScrollMargin(5, 5);
       }
    }
 }
 
void SetAutoScrollMargins()
{
   /* If the text box is outside the panel's bounds, 
          turn on auto-scrolling and set the margin. */
   if ( text1->Location.X > panel1->Location.X || text1->Location.Y > panel1->Location.Y )
   {
      panel1->AutoScroll = true;

      /* If the AutoScrollMargin is set to less 
                than (5,5), set it to 5,5. */
      if ( panel1->AutoScrollMargin.Width < 5 || panel1->AutoScrollMargin.Height < 5 )
      {
         panel1->SetAutoScrollMargin( 5, 5 );
      }
   }
}
private void SetAutoScrollMargins()
{
    /* If the text box is outside the panel's bounds, 
       turn on auto-scrolling and set the margin. */
    if (text1.get_Location().get_X() > panel1.get_Location().get_X() 
        || text1.get_Location().get_Y() > panel1.get_Location().get_Y()) {
        
        panel1.set_AutoScroll(true);

        /* If the AutoScrollMargin is set to less 
           than (5,5), set it to 5,5. */
        if (panel1.get_AutoScrollMargin().get_Width() < 5 
            || panel1.get_AutoScrollMargin().get_Height() < 5) {
                panel1.SetAutoScrollMargin(5, 5);
        }
    }
} //SetAutoScrollMargins

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

ScrollableControl 类
ScrollableControl 成员
System.Windows.Forms 命名空间
ScrollableControl.AutoScroll 属性
SetAutoScrollMargin
AutoScrollPosition
AutoScrollMinSize
DisplayRectangle